我正在尝试搜索标题以“n”开头的笔记,但我不知道我的查询有什么问题我有强制关闭错误。
这是我的日志:
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;
}
答案 0 :(得分:1)
你需要文字之间的文字
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);
mCursor.moveToFirst();
return mCursor;
}
答案 1 :(得分:1)
或者只是改变一下:
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);
答案 2 :(得分:0)
查询应该是:
select * from 'product' where product_name like 'crocin%'
在查询中添加' (single quote)
后,它应该正常运行。