4

我正在尝试从我的SQLite数据库中检索一些信息以获取 Android 应用程序的登录页面。从数据库中检索的代码是:

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

但我不断收到此错误:

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)
4

3 回答 3

20

不要将替换字符括在引号中:

WHERE name=? AND password=?

使用name='?'将尝试与name文字问号进行比较...

于 2013-03-12T20:45:09.573 回答
5

没有回答您提出的问题,而是您尚未提出的问题……您正在 UI 线程上执行数据库操作。肯定是不行的,考虑用AsyncTask把这个数据库操作代码放在里面而不是onClick里面。

于 2013-03-12T20:54:09.873 回答
2

我也得到了类似的错误。

问题是相等运算符周围的空格。

通过删除空格来解决WHERE fieldName = ?问题WHERE fieldName=?

于 2014-04-29T05:44:56.697 回答