Android 方法查询数据库以返回结果
应用程序无法运行。这种情况反复发生:Android 方法查询数据库以返回结果,android,sqlite,Android,Sqlite,应用程序无法运行。这种情况反复发生: 04-05 21:29:09.570: E/AndroidRuntime(1069): Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters. 主调用方法 Cursor wow = db.trying("G
04-05 21:29:09.570: E/AndroidRuntime(1069): Caused by: java.lang.IllegalArgumentException:
Cannot bind argument at index 1 because the index is out of range.
The statement has 0 parameters.
主调用方法
Cursor wow = db.trying("Gold");
text = (TextView) findViewById(R.id.textView13);
String quantity = wow.getString(0); //
text.setText(quantity);
DB处理程序-方法
public Cursor trying(String vg){
String q = "SELECT quantity FROM " + TABLE_CONTACTS + " WHERE name=" + "'" + vg +"'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(q, new String[] {vg});
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
问题是
String q = "SELECT quantity FROM " + TABLE_CONTACTS + " WHERE name=" + "'" + vg +"'";
在查询中,您已经为where条件指定了参数。之后,您将再次将其传递给查询
Cursor cursor = db.rawQuery(q, new String[] {vg});
这会造成混乱。所以请尝试更改您的查询
String q = "SELECT quantity FROM " + TABLE_CONTACTS + " WHERE name = ?";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(q, new String[] {vg});
或者你选择另一种方法
String q = "SELECT quantity FROM " + TABLE_CONTACTS + " WHERE name=" + "'" + vg +"'";
Cursor cursor = db.rawQuery(q, null);
if(cursor != null && cursor.getCount()>0){
cursor.moveToFirst();
//do your action
//Fetch your data
}
else {
Toast.makeText(getBaseContext(), "No records yet!", Toast.LENGTH_SHORT).show();
return;
}
我不明白你为什么创建下一个问题,而不是在上一个问题中引用此错误并给我下一个评论。