0

应用程序不会运行。这经常发生:

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.

Main - 调用方法

Cursor wow = db.trying("Gold");
       text = (TextView) findViewById(R.id.textView13);
       String quantity = wow.getString(0); //
       text.setText(quantity);

数据库处理程序 - 方法

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;
    }
4

1 回答 1

1

问题是

 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;
}  

参考

于 2013-04-06T10:13:07.910 回答