2013-05-02 215 views
2

我有一個數據庫和一個方法來從每個表中獲取所有值。Sqlite數據庫查詢返回null

不過,我想只得到錶行,其中路線= 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 
+0

查詢有去無回'null' ,而是它將返回空的光標 – Pragnani 2013-05-02 15:51:56

+0

你的數據庫表中的路由類型是什麼?是Text還是INTEGER? – Pragnani 2013-05-02 15:55:50

+0

類型的路線是TEXT – Maggie 2013-05-02 16:00:37

回答

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); 
+0

它工作得更好我想! 但現在我收到一個錯誤,說:「05-02 18:07:21.111:E/SQLiteLog(15019):(1)沒有這樣的列:5 – Maggie 2013-05-02 16:08:05

+0

@Maggie Post在問題中完整的堆棧跟蹤.. – Pragnani 2013-05-02 16:12:51

+0

我用另一個符號在回答中,而不是單引號在條件statemet中使用這個''route =「+」'「+ busroute +」'「' – Pragnani 2013-05-02 16:15:53