1

我有一个数据库和一个方法来从每个表中获取所有值。

但是,我只想获取 route = 5 的表行(例如)

这是我的查询:

  Cursor cursor = db.query(TABLE_NAME_ROUTES, new String[] {"id","route","busstop","longitude","latitude"}, "route" + "=" + busroute , null, null, null, null); 

我已经尝试了很多不同版本的 WHERE 子句,但似乎都不起作用,因为返回的列表大小为 0,而它的大小应该约为 20。

编辑:

将行更改为:

 Cursor cursor = db.query(TABLE_NAME_ROUTES, new String[] {"id","route","busstop","longitude","latitude"}, "route" + "=`"+ busroute+"`" , null, null, null, null);

来自 Logcat 的日志

05-02 18:14:23.396: E/SQLiteLog(15408): (1) no such column: 5

05-02 18:14:23.396: D/AndroidRuntime(15408): Shutting down VM
05-02 18:14:23.396: W/dalvikvm(15408): threadid=1: thread exiting with uncaught exception (group=0x40cee300)
05-02 18:14:23.400: E/AndroidRuntime(15408): FATAL EXCEPTION: main
05-02 18:14:23.400: E/AndroidRuntime(15408): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mso.master/com.mso.master.MainActivity}: android.database.sqlite.SQLiteException: no such column: 5 (code 1): , while compiling: SELECT id, route, busstop, longitude, latitude FROM routes WHERE route=`5`
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.app.ActivityThread.access$600(ActivityThread.java:142)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.os.Looper.loop(Looper.java:137)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.app.ActivityThread.main(ActivityThread.java:4931)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at java.lang.reflect.Method.invokeNative(Native Method)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at java.lang.reflect.Method.invoke(Method.java:511)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at dalvik.system.NativeStart.main(Native Method)
05-02 18:14:23.400: E/AndroidRuntime(15408): Caused by: android.database.sqlite.SQLiteException: no such column: 5 (code 1): , while compiling: SELECT id, route, busstop, longitude, latitude FROM routes WHERE route=`5`
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at com.mso.master.DataManipulator.selectRoute(DataManipulator.java:167)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at com.mso.master.DBaccess.getBusRoute(DBaccess.java:43)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at com.mso.master.MainActivity.onCreate(MainActivity.java:130)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.app.Activity.performCreate(Activity.java:5008)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-02 18:14:23.400: E/AndroidRuntime(15408):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139)
05-02 18:14:23.400: E/AndroidRuntime(15408):    ... 11 more
4

1 回答 1

0

route类型是text,所以它应该在那里single quotes,试试这个

Cursor cursor = db.query(TABLE_NAME_ROUTES, new String[] {"id","route","busstop","longitude","latitude"}, "route" + "='"+ busroute+"'" , null, null, null, null);
于 2013-05-02T16:02:50.483 回答