SQLite中所有SQL语句都需要先编译为stmt,然后执行。
首先构造SQLiteStatement对象,然后使用该对象执行,通过session调用到连接池中某个connection的execute相关方法。
其中,在connection中重新构建PreparedStatement,其实该对象才真正指向sqlite中的stmt。
1、使用query的方式
1
2
3
4
5
6
7
8
9
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Cursor cr;
cr = db.rawQuery(
"select * from person where name=yuu"
,
null
);
if
(cr.moveToFirst()) {
for
(
int
i =
0
; i < cr.getCount(); i++) {
cr.getString();
cr.moveToNext();
}
}
|
但当出现特殊字符时,会报sql异常建议使用第二种方案
2、使用带问号的查询方式
1
| StringBuffer selection = new StringBuffer(); selection.append(Media.SIZE).append(">0").append(" AND "); selection.append(Media.BUCKET_DISPLAY_NAME).append("=?"); String[] selectionArgs = new String[]{dirName}; //selection.append(Media.BUCKET_DISPLAY_NAME).append("='").append(dirName).append("'"); try { Cursor cursor = context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, columns, selection.toString(), selectionArgs, Media.DISPLAY_NAME); StringBuffer selection = new StringBuffer(); |