2 sachinks SachinKS 于 2013.04.01 17:16 提问

在使用SQL 查询时强制关闭

在查询标题开头为 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;
 }

1个回答

guoleimail
guoleimail   2013.04.01 18:03
已采纳

请在 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);
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!