0

App won't run. This occurs recurrently:

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 - Calling method

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

DB Handler - Method

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; } 
1
  • I don't understand why you created next question instead of refer this error in previous one and give me next comment. Commented Apr 5, 2013 at 21:46

1 Answer 1

1

The problem is

 String q = "SELECT quantity FROM " + TABLE_CONTACTS + " WHERE name=" + "'" + vg +"'"; 

In your query you had already specify the parameter for where condition .After that you are again passing it to query

Cursor cursor = db.rawQuery(q, new String[] {vg}); 

This makes the confusion .So try to change your query

String q = "SELECT quantity FROM " + TABLE_CONTACTS + " WHERE name = ?"; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(q, new String[] {vg}); 

OR you go for another approach

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

Refer

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.