sql语句如果不经常用如果突然要写一条sql, 可能会花一些时间在网上找, 而在网上找真的有点儿太浪费时间, 有这时间又可以去学习自己陌生的领域, 虽然有w3school, 但是也不太全. 题外话, 总感觉sql和多线程是Android开发者的硬伤;
demo已提交github
1. 建表:
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table Test("
+ "_id integer primary key autoincrement,"
+ "ParamA text,"
+ "ParamB varchar(20),"
+ "ParamC varchar(20))";
db.execSQL(sql);
}
2. 插入操作:
1. 方式一(insert):
2. 方式二(execSQL):
- 1. 方式一(insert):
public void addByInsert(ParamsEngine engine) {
SQLiteDatabase database = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("ParamA", engine.mParamA);
values.put("ParamB", engine.mParamB);
values.put("ParamC", engine.mParamC);
database.insert("Test", null, values);
}
- 2. 方式二(execSQL):
(1)
insert into Test(ParamA, ParamB, ParamC) values (?,?,?)
public void addByExecSQLAndRes(ParamsEngine engine) {
SQLiteDatabase database = mHelper.getWritableDatabase();
String sql = ComponentUtils.getContext().getString(R.string.addByExecSQL);
String[] bingArgs = new String[]{engine.mParamA, engine.mParamB, engine.mParamC};
database.execSQL(sql, bingArgs);
}
(2)
public void addByExecSQL(ParamsEngine engine) {
SQLiteDatabase database = mHelper.getWritableDatabase();
String sql = "insert into Test(ParamA, ParamB, ParamC) values (?,?,?)";
String[] bingArgs = new String[]{engine.mParamA, engine.mParamB, engine.mParamC};
database.execSQL(sql, bingArgs);
}
3. 删除操作:
1. DELETE FROM TableContainer WHERE ParamA IN ("paramA");
4. 查找:
1. in限定符查找:
select count(*) from TableContainer where ParamA in ("ParamA_1","ParamA_3","ParamA_5")
select ParamC from TableContainer where ParamA in ("ParamA_1","ParamA_3","ParamA_5") and ParamB in ("ParamB_3")
select * from TableContainer where ParamA in ("ParamA_1","ParamA_3","ParamA_5") and ParamB in ("ParamB_3")
2. 分页查询:
http://blog.csdn.net/lu1024188315/article/details/51734514
3. join:
sqlite不支持右连接和外连接:
左连接:
select * from tableA a left join tableB b on a.aId = b.bId;
以左表A为基础;