在查詢標題開頭為 n 的記錄時,出現了強制關閉。
log:
04-01 12:19:37.833: D/AndroidRuntime(676): Shutting down VM
04-01 12:19:37.833: W/dalvikvm(676): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-01 12:19:37.913: E/AndroidRuntime(676): FATAL EXCEPTION: main
04-01 12:19:37.913: E/AndroidRuntime(676): android.database.sqlite.SQLiteException: near "%": syntax error: , while compiling: SELECT DISTINCT _id, title, body, cdate, passw FROM notes WHERE title LIKE n%
04-01 12:19:37.913: E/AndroidRuntime(676): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-01 12:19:37.913: E/AndroidRuntime(676): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
代碼:
public Cursor search(String title) {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_TITLE, KEY_BODY,KEY_CDATE,KEY_PASS}, KEY_TITLE + " LIKE" +" " + title + '%', null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
請在 n% 前後加上引號,如:
SELECT DISTINCT _id, title, body, cdate, passw FROM notes WHERE title LIKE “n%”
至於你說的代碼,可以這樣寫:
mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_TITLE, KEY_BODY,KEY_CDATE,KEY_PASS}, KEY_TITLE + " LIKE '" +" " + title + "%'", null,
null, null, null, null);