hamod90
hamod90

Reputation: 111

Parameterized query throws IllegalArgumentException: Cannot bind argument

I'm trying to retrieve some info out of my SQLite database for a login page for an Android application. The code for retrieving from a db is:

login.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.androidproject/databases/myDB", null, 0);
            // Check Login
            String username = usernamelogin.getText().toString();
            String password = loginpassword.getText().toString();

            Cursor c = db.rawQuery("SELECT name, password FROM User WHERE name='?' AND password='?'", new String[] {username, password});
            if(c.moveToFirst()) {
                Intent intent=new Intent(Login.this, MainActivity.class);
                 startActivity(intent);
            } else {
                Toast.makeText(getApplicationContext(), "Invalid", Toast.LENGTH_LONG).show();
            }
            c.close();
            db.close();
        }
    });  

But I keep getting this error:

03-12 20:38:40.216: E/AndroidRuntime(691): FATAL EXCEPTION: main
03-12 20:38:40.216: E/AndroidRuntime(691): java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range.  The statement has 0 parameters.
03-12 20:38:40.216: E/AndroidRuntime(691):  at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212)
03-12 20:38:40.216: E/AndroidRuntime(691):  at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166)
03-12 20:38:40.216: E/AndroidRuntime(691):  at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
03-12 20:38:40.216: E/AndroidRuntime(691):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
03-12 20:38:40.216: E/AndroidRuntime(691):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
03-12 20:38:40.216: E/AndroidRuntime(691):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
03-12 20:38:40.216: E/AndroidRuntime(691):  at com.example.androiddiet.Login$2.onClick(Login.java:61)
03-12 20:38:40.216: E/AndroidRuntime(691):  at android.view.View.performClick(View.java:4084)
03-12 20:38:40.216: E/AndroidRuntime(691):  at android.view.View$PerformClick.run(View.java:16966)
03-12 20:38:40.216: E/AndroidRuntime(691):  at android.os.Handler.handleCallback(Handler.java:615)
03-12 20:38:40.216: E/AndroidRuntime(691):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 20:38:40.216: E/AndroidRuntime(691):  at android.os.Looper.loop(Looper.java:137)
03-12 20:38:40.216: E/AndroidRuntime(691):  at android.app.ActivityThread.main(ActivityThread.java:4745)
03-12 20:38:40.216: E/AndroidRuntime(691):  at java.lang.reflect.Method.invokeNative(Native Method)
03-12 20:38:40.216: E/AndroidRuntime(691):  at java.lang.reflect.Method.invoke(Method.java:511)
03-12 20:38:40.216: E/AndroidRuntime(691):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-12 20:38:40.216: E/AndroidRuntime(691):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-12 20:38:40.216: E/AndroidRuntime(691):  at dalvik.system.NativeStart.main(Native Method)

Upvotes: 4

Views: 4490

Answers (3)

Mohammed H
Mohammed H

Reputation: 7048

I also got the similar error.

The issue was spaces around the equal operator.

Fixed the issue by removing the spaces from WHERE fieldName = ? to WHERE fieldName=?

Upvotes: 2

Sam
Sam

Reputation: 86948

Don't wrap the replacement character in quotes:

WHERE name=? AND password=?

Using name='?' will try to compare name to a literal question mark...

Upvotes: 20

Alécio Carvalho
Alécio Carvalho

Reputation: 13647

Not answering the question you've asked, but a question you still haven't asked...you're performing database operation on the UI thread. It is definitely not the way to do it, consider using AsyncTask to place this database operation code in instead of in the onClick.

Upvotes: 5

Related Questions