SQLite-CursorIndexOutofBound Exception-Android

时间:2013-12-06 10:45:13

标签: android database sqlite

DBAdapter.java

 public void onCreate(SQLiteDatabase db) {

                        db.execSQL(" CREATE TABLE " + DATABASE_TABLE + " (" + KEY_FIRSTNAME
                                + " TEXT NOT NULL, " + KEY_MIDDLENAME + " TEXT NOT NULL, "
                                + KEY_LASTNAME + " TEXT NOT NULL, " + KEY_USERNAME + " TEXT NOT NULL, " + KEY_MAIL_ID + " TEXT NOT NULL, "
                                + KEY_PASSWORD + " TEXT NOT NULL, " + KEY_CONFIRM
                                + " TEXT NOT NULL, " + KEY_DATE_OF_BIRTH
                                + " INTEGER NOT NULL "
                                + ");");

                    }

public String getSinlgeEntry(String userName) {

                Cursor cursor = mDB.query(DATABASE_TABLE,null,KEY_USERNAME+" =?",new String[]{userName},null,null,null);
                if (cursor.getCount() < 1) // UserName Not Exist
                    return "NOT EXIST";
                cursor.moveToFirst();
                String password = cursor.getString(cursor
                        .getColumnIndex("KEY_PASSWORD"));
                return password;

            }

MainActivity.java

    String stored_password = dbadapter.getSinlgeEntry(username);
                        Log.i("MainActivity", "The stored password is" + stored_password);
                        if(password.equals(stored_password))
                        {
                            Toast.makeText(MainActivity.this,"It is logging in",Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(MainActivity.this,Profile_view.class);
                            startActivity(intent);
                        }
                        else
                        {
                            Toast.makeText(MainActivity.this,"Either username or password in invalid",Toast.LENGTH_LONG).show();
                        }

logcat的:

12-06 16:05:47.682: E/AndroidRuntime(584): FATAL EXCEPTION: main
12-06 16:05:47.682: E/AndroidRuntime(584): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
12-06 16:05:47.682: E/AndroidRuntime(584):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:419)
12-06 16:05:47.682: E/AndroidRuntime(584):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:117)
12-06 16:05:47.682: E/AndroidRuntime(584):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:31)
12-06 16:05:47.682: E/AndroidRuntime(584):  at com.example.signup.DBAdapter.getSinlgeEntry(DBAdapter.java:177)
12-06 16:05:47.682: E/AndroidRuntime(584):  at com.example.signup.MainActivity$1.onClick(MainActivity.java:46)
12-06 16:05:47.682: E/AndroidRuntime(584):  at android.view.View.performClick(View.java:3100)
12-06 16:05:47.682: E/AndroidRuntime(584):  at android.view.View$PerformClick.run(View.java:11644)
12-06 16:05:47.682: E/AndroidRuntime(584):  at android.os.Handler.handleCallback(Handler.java:587)
12-06 16:05:47.682: E/AndroidRuntime(584):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-06 16:05:47.682: E/AndroidRuntime(584):  at android.os.Looper.loop(Looper.java:126)
12-06 16:05:47.682: E/AndroidRuntime(584):  at android.app.ActivityThread.main(ActivityThread.java:3997)
12-06 16:05:47.682: E/AndroidRuntime(584):  at java.lang.reflect.Method.invokeNative(Native Method)
12-06 16:05:47.682: E/AndroidRuntime(584):  at java.lang.reflect.Method.invoke(Method.java:491)
12-06 16:05:47.682: E/AndroidRuntime(584):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-06 16:05:47.682: E/AndroidRuntime(584):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-06 16:05:47.682: E/AndroidRuntime(584):  at dalvik.system.NativeStart.main(Native Method)

我的桌子上有3个完整的entries(firstname,middlename,lastname..)。所有的 用户名已存在于数据库中,但它表示CursorIndexOutOfBoundException
我也将光标移动到第一个位置。

1 个答案:

答案 0 :(得分:4)

KEY_PASSWORD是定义键列名的常量,而不是列名本身。

替换

String password = cursor.getString(cursor
                    .getColumnIndex("KEY_PASSWORD"));

String password = cursor.getString(cursor
                    .getColumnIndex(KEY_PASSWORD));