getWriteableDatabase()引起的Android SQLite错误

时间:2014-10-26 20:17:18

标签: android sqlite

我是Android的新手,我正在尝试创建一个包含用于存储电子邮件地址,用户名和密码的表的数据库。在对程序应用断点之后,以下代码行似乎引发了问题:

SQLiteDatabase database = this.getWriteableDatabase();

以下是围绕上述行的代码:

// Create A User
public boolean createUser(String userEmail, String userName, String userPassword) {
    ContentValues values = new ContentValues();
    values.put(KEY_USER_EMAIL, userEmail);
    values.put(KEY_USER_NAME, userName);
    values.put(KEY_USER_PASSWORD, userPassword);

    SQLiteDatabase database = this.getWritableDatabase();

    database.close();
}

尝试创建用户时出现以下错误:

java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3052)
            at android.view.View.performClick(View.java:3519)
            at android.view.View$PerformClick.run(View.java:14140)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3047)
            at android.view.View.performClick(View.java:3519)
            at android.view.View$PerformClick.run(View.java:14140)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: database /data/data/com.pastpapercreator.chemistrypastpapercreator/databases/chemistryPastPaperCreator (conn# 0) already closed
            at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)
            at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2088)
            at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:690)
            at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:176)
            at com.pastpapercreator.chemistrypastpapercreator.helper.DatabaseHelper.createUser(DatabaseHelper.java:111)
            at com.pastpapercreator.chemistrypastpapercreator.CreateAccount.createAccount(CreateAccount.java:83)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3047)
            at android.view.View.performClick(View.java:3519)
            at android.view.View$PerformClick.run(View.java:14140)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
            at dalvik.system.NativeStart.main(Native Method)

非常感谢任何帮助。

以下是用于创建数据库的代码:

// User Table Column Names
private static final String KEY_USER_ID = "uId";
private static final String KEY_USER_EMAIL = "uEmail";
private static final String KEY_USER_NAME = "uName";
private static final String KEY_USER_PASSWORD = "uPassword";

// User Table Creation Statement
private static final String CREATE_TABLE_USER = "CREATE TABLE " + TABLE_USER +
"(" + KEY_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_USER_EMAIL + " TEXT," + KEY_USER_NAME + " TEXT," + KEY_USER_PASSWORD + " TEXT" + ")";

public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

@Override
    public void onCreate(SQLiteDatabase database) {
        // Create Required Tables
        database.execSQL(CREATE_TABLE_USER);

        database.close();
    }

1 个答案:

答案 0 :(得分:0)

在您的数据库助手类上尝试此操作 检查您的克里特岛查询

// Database creation sql statement
  private static final String DATABASE_CREATE = "create table "
  + TABLE_COMMENTS + "(" + COLUMN_ID
  + " integer primary key autoincrement, " + COLUMN_COMMENT
  + " text not null);";

   public MySQLiteHelper(Context context) {
  super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }
  @Override
  public void onCreate(SQLiteDatabase database) {
  database.execSQL(DATABASE_CREATE);
  }