我正在尝试从我自己的数据库中获取一行,位于assets文件夹中。检查表中是否有一行,使用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;
}
}
希望有人可以帮助我。非常感谢提前!
答案 0 :(得分:2)
好的首先,我要感谢大家的快速回复。保持!虽然答案很明显,但我自己也明白了。来自:http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/的教程中的问题是,它不考虑在首次运行程序后手动将值(在SQLite数据库浏览器中更新)添加到数据库的情况。这就是我所做的......所以你必须编写自己的方法来检查自上次运行以来是否有任何变化。希望任何人都需要这个:))
答案 1 :(得分:1)
//这对我有用
final Cursor c = sampleDB.rawQuery("SELECT FirstName, LastName,RedId FROM " +
SAMPLE_TABLE_NAME +
" where RedId = '"+a+"'", null);
int count = c.getCount();
if(count == 0){
Toast.makeText(RetrieveData.this, "Not Found", Toast.LENGTH_SHORT).show();
}
答案 2 :(得分:0)
尝试使用此功能从数据库中获取单行。
在您的数据库类中声明此方法。
public Cursor selectSingleRecord(String tableName, String[] tableColumns,
String whereClause, String whereArgs[], String groupBy,
String having, String orderBy) {
return myDataBase.query(tableName, tableColumns, whereClause, whereArgs,
groupBy, having, orderBy);
}
并使用此方法获取光标。
如果我的表结构如下。
_id |名称|地址
我会用以下方式写作
String tableColumns[] = {"_id"}; // Here mansion the column which you want to retrieve I think in your case "addressString".
String whereClause = {"Name"}; // Here use where clause
Cursor cursor = dbHelper.selectSingleRecord("Your_Table_Name", tableColumns, whereClase +"=?" , new String[] {String.valueOf("NameOfPerson")}, null, null, null);
// NameOfperson is the name which I am going to enter through EditText.
if (cursor.moveToFirst())
{
String getId = cursor.getString[0]; // Here you can get the id of respective person (i.e. the name which I have entered )
}
cursor.close(); // Finally close the curser
希望这会对你有所帮助。
答案 3 :(得分:0)
将您的sSelect更改为:有时在SQL中,最好使用“某物”而非“某事物”。
String sSelect =“SELECT * FROM”+ table +“WHERE”+ column +“喜欢'”+搜索+“'”;
答案 4 :(得分:0)
不知道这可能是问题但是。 。 。
String sSelect = "SELECT * FROM " + table + " WHERE " + column
+ " = '" + search + "'";
当您的搜索变量为null时,您的查询将转换为:
SELECT * FROM tblBar WHERE Adresse ='null'
尝试使用此代码:
search = (search == null) ? " IS NULL " : " = '" + search +" '" ;
String sSelect = "SELECT * FROM " + table + " WHERE " + column + search;