我目前正試圖從我自己的數據庫中找到一個單獨的行,位於資產文件夾中。檢查表中是否有行,SQLite查詢瀏覽器和調試器的SELECT語句讓我在數據庫中有一行。無論如何,從我的代碼調用聲明只是讓我一個空的光標。我還添加方法Android遊標返回沒有行的選擇查詢
cursor.moveToFirst()
和避免呼叫
myDataBase.close();
這裏就是我想要追加對一個TextView單個條目的方法:
private void getAdresses(Location location) { // TODO
double lat = location.getLatitude();
double lng = location.getLongitude();
Geocoder geocoder = new Geocoder(this, Locale.GERMAN);
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
if (addresses.size() != 0) {
Address address = addresses.get(0);
String addressString = address.getAddressLine(0).toString();
System.out.println("(geocoder) " + addressString);
textView.append(addressString + "\n");
Cursor cursor = mDbHelper.getAllEntrys("tblBar", "Adresse",
addressString);
if (cursor.moveToFirst()) {
String cursorContent = cursor.getString(cursor
.getColumnIndexOrThrow("Name"));
textView.append(cursorContent + "\n");
} else {
System.out.println("No bar at current location");
}
} else {
System.out.println("(geocoder) no address!");
}
} catch (IOException e) {
System.out.println(e.toString() + "(geocoder)");
}
}
而且這是我調用數據庫的實際查詢方法:
public Cursor getAllEntrys(String table, String column, String search) {
try {
String sSelect = "SELECT * FROM " + table + " WHERE " + column
+ " = '" + search + "'";
Cursor cursor = mDb.rawQuery(sSelect, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
} catch (SQLException mSQLException) {
System.out.println("getData >>" + mSQLException.toString());
throw mSQLException;
}
}
希望有人能幫助我。提前謝謝了!
嘗試調用DatabaseUtils.dumpCursor(光標)後,立即光標光標= mDb.rawQuery(S選擇,NULL);並檢查logcat的轉儲。它會告訴你遊標是否有任何內容。 – Samuel
感謝您的快速回答。我試着轉儲光標,它告訴我,我的光標沒有內容。問題是我不知道爲什麼。 – user1622601
首先確保數據庫連接沒有問題。然後在運行查詢之前記錄sSelect。這將幫助您在查詢中發現錯誤。您應該可以將日誌消息正確複製到SQLite客戶端中,而不會出現任何問題。 – Samuel