我有一個SQLite數據庫,我需要從中獲取一些值。我需要獲取經緯度來在地圖上放置一個地理點,但是我收到了一個錯誤。這是我的logcat:從SQLITE DB獲取值時出錯
05-07 14:17:52.811 10811-10811/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.disasternew, PID: 10811
android.database.sqlite.SQLiteException: near "SELECT": syntax error (code 1): , while compiling: SELECT shelter_id, title, desc, lat, long, route, capacity, current_load FROM Shelter WHERE SELECT title,desc,lat,long,route,capacity,current_load from Shelter
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:898)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1346)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1193)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1064)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1270)
at com.disasternew.MainActivity.doSearch(MainActivity.java:117)
at com.disasternew.MainActivity$1.onClick(MainActivity.java:93)
at android.view.View.performClick(View.java:4848)
at android.view.View$PerformClick.run(View.java:20262)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
查詢代碼:
String querySelect = "SELECT title,desc,lat,long,route,capacity,current_load from Shelter";
Log.e("test", "-----" + querySelect);
Cursor locationCursor = database.query("Shelter", new String[]{"shelter_id","title", "desc", "lat", "long","route","capacity","current_load"},querySelect, null, null,
null, null, null);
locationCursor.moveToFirst();
Log.e("test", querySelect);
while (locationCursor.moveToNext()) {
String title = locationCursor.getString(locationCursor.getColumnIndex("title"));
String desc = locationCursor.getString(locationCursor.getColumnIndex("desc"));
lattitude_shelter = (int) (locationCursor.getFloat(locationCursor.getColumnIndex("lat")) * 1E6);
longitude_shelter = (int) (locationCursor.getFloat(locationCursor.getColumnIndex("long")) * 1E6);
}
創建表:
public void onCreate(SQLiteDatabase db) {
String CREATE_SHELTER_TABLE = "Create table Shelter("+
" shelter_id INTEGER AUTO INCREMENT PRIMARY KEY, "+
" title TEXT ,"+
" desc TEXT, "+
" lat Float,"+
" long Float ,"+
" route TEXT ,"+
" capacity INT,"+
" current_load INT)";
db.execSQL(CREATE_SHELTER_TABLE);
db.execSQL(" INSERT INTO Shelter VALUES(1,'College More','Salt Lake',22.57,88.43,'Open',10,5);");
db.execSQL(" INSERT INTO Shelter VALUES(2,'Salt Lake Stadium','Salt Lake',22.56,88.40,'Open',10,8);");
db.execSQL(" INSERT INTO Shelter VALUES(3,'College More','Salt Lake',22.56,88.40,'Full',10,10);");
}
我猜你的'querySelect'是錯誤的。 query方法的第三個參數必須是WHERE子句(沒有WHERE本身),在那裏你定義了哪些行應該返回以及爲什麼。例如,你說:給我所有標題爲College More的行。然後它是這樣的:'String whereClause =「title =?」'然後在args中傳遞你正在搜索的值:'String [] whereArgs = new String [] { 「College More」} – Opiatefuchs
@Opiatefuchs我沒有在這裏使用where子句。我希望所有的行沒有任何過濾,並且從內置的SQLITEDatabase.class中使用的方法是public Cursor query(String table,String [] columns,String selection,String [] selectionArgs ,String groupBy,String having,String orderBy,String limit){newThreadEvent(「Stub!」); } –
@Opiatefuchs我也試過rawquery,然後顯示「android.database.sqlite.SQLiteException:沒有這樣的列:拉特(代碼1):,而編譯:SELECT title,desc,lat,long,route,capacity,current_load FROM Shelter「 –