我目前正在尝试从我自己的数据库中获取一行,该数据库位于资产文件夹中。检查表中是否有一行,使用 SQLite 查询浏览器和调试器中的 SELECT-Statement 让我在数据库中找到一行。无论如何,从我的代码中调用该语句只会让我得到一个空游标。我还添加了方法
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;
}
}
希望有人可以帮助我。提前谢谢了!