0

我有一个 sqlite 语句,但它在 where 子句中给了我错误,并且 where 子句中的函数“sres.getsrthreadid”最后返回给我带有某个数字的 mac 地址。我的功能是:

SResponses sres=new SResponses();
String queryz = "SELECT " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_MESSAGE+","+MySQLiteHelper.COLUMN_THREADID+","+MySQLiteHelper.COLUMN_POSITION + " FROM " + MySQLiteHelper.TABLE_NAME + " WHERE "+MySQLiteHelper.COLUMN_THREADID+ "="+sres.getsrthreadid();

Log.e("datasource", sres.getsrthreadid());
cursors = database.rawQuery(queryz, null);

Logcat 是:

11-07 11:50:35.721: E/datasource(14881): b8:b4:2e:36:6c:6c1
11-07 11:50:35.721: I/SqliteDatabaseCpp(14881): sqlite returned: error code = 1, msg = near ":b4": syntax error, db=/data/data/soft.b.peopleassist/databases/sentitems.db
11-07 11:50:35.722: D/AndroidRuntime(14881): Shutting down VM
11-07 11:50:35.722: W/dalvikvm(14881): threadid=1: thread exiting with uncaught exception (group=0x40e42258)
11-07 11:50:35.731: E/AndroidRuntime(14881): FATAL EXCEPTION: main
11-07 11:50:35.731: E/AndroidRuntime(14881): java.lang.RuntimeException: Unable to start activity ComponentInfo{soft.b.peopleassist/soft.b.peopleassist.SResponses}: android.database.sqlite.SQLiteException: near ":b4": syntax error: , while compiling: SELECT id,message,threadid,position FROM message WHERE threadid=b8:b4:2e:36:6c:6c1
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2084)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2111)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.app.ActivityThread.access$600(ActivityThread.java:134)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1251)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.os.Looper.loop(Looper.java:137)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.app.ActivityThread.main(ActivityThread.java:4666)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at java.lang.reflect.Method.invokeNative(Native Method)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at java.lang.reflect.Method.invoke(Method.java:511)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at dalvik.system.NativeStart.main(Native Method)
11-07 11:50:35.731: E/AndroidRuntime(14881): Caused by: android.database.sqlite.SQLiteException: near ":b4": syntax error: , while compiling: SELECT id,message,threadid,position FROM message WHERE threadid=b8:b4:2e:36:6c:6c1
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:177)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:395)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1538)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at soft.b.peopleassist.DataSource.getAllMessages(DataSource.java:173)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at soft.b.peopleassist.SResponses.onCreate(SResponses.java:40)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.app.Activity.performCreate(Activity.java:4510)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
11-07 11:50:35.731: E/AndroidRuntime(14881):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2048)
11-07 11:50:35.731: E/AndroidRuntime(14881):    ... 11 more
4

2 回答 2

2

引用您的输入值,以便 SQLite 可以正确获取它:

String queryz = "SELECT " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_MESSAGE+","+MySQLiteHelper.COLUMN_THREADID+","
+MySQLiteHelper.COLUMN_POSITION + " FROM " + MySQLiteHelper.TABLE_NAME + " WHERE "
+MySQLiteHelper.COLUMN_THREADID+ "='"+sres.getsrthreadid() + "'; // Here
于 2013-11-07T06:50:45.843 回答
2

只需用此替换您的代码

SResponses sres=new SResponses();
String queryz = "SELECT " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_MESSAGE+","+MySQLiteHelper.COLUMN_THREADID+","+MySQLiteHelper.COLUMN_POSITION + " FROM " + MySQLiteHelper.TABLE_NAME + " WHERE "+MySQLiteHelper.COLUMN_THREADID+ "='" + sres.getsrthreadid() + "'";

Log.e("datasource", sres.getsrthreadid());
cursors = database.rawQuery(queryz, null);
于 2013-11-07T06:54:49.527 回答