android中的Sqlite查询出错

时间:2015-04-07 17:44:38

标签: android android-sqlite

我需要在列中找到一个字符串并返回包含该值的另一​​列。我有字符串值“xyx”..空间正在创建未知字符所以我想看看它是否只有xyz我收到以下错误

cursor = sqlDb.query(MYTABLE,
    thecolumns, NAME + " LIKE '" + name + "'", null, null, null, null);

04-07 10:31:38.302: W/dalvikvm(18987): threadid=1: thread exiting with uncaught exception (group=0x41884da0)
04-07 10:31:38.372: E/AndroidRuntime(18987): FATAL EXCEPTION: main
04-07 10:31:38.372: E/AndroidRuntime(18987): Process: com.myproject, PID: 18987
04-07 10:31:38.372: E/AndroidRuntime(18987): android.database.sqlite.SQLiteException: unrecognized token: "'%xyz" (code 1): , while compiling: **SELECT column FROM TABLE_NAME WHERE Name LIKE '%xyz��������������������������������������������������������������������%'**
04-07 10:31:38.372: E/AndroidRuntime(18987):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-07 10:31:38.372: E/AndroidRuntime(18987):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
04-07 10:31:38.372: E/AndroidRuntime(18987):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:690)
04-07 10:31:38.372: E/AndroidRuntime(18987):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-07 10:31:38.372: E/AndroidRuntime(18987):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
04-07 10:31:38.372: E/AndroidRuntime(18987):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
04-07 10:31:38.372: E/AndroidRuntime(18987):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
04-07 10:31:38.372: E/AndroidRuntime(18987):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1435)
04-07 10:31:38.372: E/AndroidRuntime(18987):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1282)
04-07 10:31:38.372: E/AndroidRuntime(18987):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1153)
04-07 10:31:38.372: E/AndroidRuntime(18987):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1321)

1 个答案:

答案 0 :(得分:0)

检查撇号是否有效,但要避免这种情况,请使用选择参数?操作

cursor = sqlDb.query(MYTABLE,
    thecolumns, NAME + " LIKE ?", new String[]{"%" + name + "%"}, null, null, null);