唯一约束失败:type.ps

时间:2019-06-17 10:03:28

标签: android mysql sqlite android-database

我的字典应用程序项目使用SQLite来存储数据。运行应用程序时,我在方法UNIQUE constraint failed: type.ps中得到了createTablesAndProcess()

createTablesAndPorcess方法:

public final void createTablesAndProcess() {
    boolean z = true;
    try {
        if (!doesExistsTable("other_words")) {
            this.wdb.execSQL("create table if not exists other_words(word TEXT PRIMARY KEY)");
            this.wdb.execSQL("INSERT INTO other_words ( word ) SELECT DISTINCT w FROM ( SELECT ot AS w FROM oten UNION SELECT word AS w FROM other )");
        }
    } catch (Exception e) {
        try {
            e.printStackTrace();
            z = false;
        } catch (Exception e2) {
            e2.printStackTrace();
            return;
        }
    }
    try {
        if (!doesExistsTable("new_study_plan")) {
            this.wdb.execSQL("create table if not exists new_study_plan(serial INTEGER PRIMARY KEY , word TEXT , add_time INTEGER NOT NULL)");
        }
    } catch (Exception unused) {
    }
    try {
        if (!doesExistsTable("new_study_plan_other")) {
            this.wdb.execSQL("create table if not exists new_study_plan_other(serial INTEGER PRIMARY KEY , word TEXT , add_time INTEGER NOT NULL)");
        }
    } catch (Exception unused2) {
    }
    try {
        if (!doesExistsTable("new_added")) {
            this.wdb.execSQL("create table if not exists new_added(word TEXT PRIMARY KEY , details TEXT  NOT NULL)");
        }
    } catch (Exception unused3) {
    }
    try {
        if (!doesExistsTable("new_history")) {
            this.wdb.execSQL("create table if not exists new_history(serial INTEGER PRIMARY KEY , word TEXT , add_time INTEGER NOT NULL)");
        }
    } catch (Exception unused4) {
    }
    if (!doesExistsTable("new_history_other")) {
        this.wdb.execSQL("create table if not exists new_history_other(serial INTEGER PRIMARY KEY , word TEXT , add_time INTEGER NOT NULL)");
    }

//Crash on the below line.
    this.wdb.execSQL("INSERT INTO type VALUES ( NULL,'MORE')");
    if (z) {
        manageInThread();
    }
}

初始化数据库任务:

private void initilizeDatabase(final int i, final boolean z) {
    new AsyncTask<String, String, String>() {
        protected void onPreExecute() {
            try {
                if (i == Constants.DB_VERSION) {
                    DictionaryActivity.this.progressMessageView.setVisibility(8);
                } else {
                    DictionaryActivity.this.progressMessageView.setVisibility(0);
                }
            } catch (Exception e) {
                LogUtils.log(DictionaryActivity.this, e);
            }
        }

        protected String doInBackground(String... strArr) {
            if (z) {
                DatabaseHelper.backupInFile(DictionaryActivity.this, i);
            }
            DatabaseHelper.manageDatabase(DictionaryActivity.this, false);
            try {
                DictionaryActivity.this.databaseAccessor = new DatabaseAccessor(DictionaryActivity.this);
            } catch (Exception e) {
                e.printStackTrace();
            }
            DictionaryActivity.this.databaseAccessor.createTablesAndPorcess();

     //Crash while executing above line.
            DictionaryActivity.this.databaseAccessor.initTypeMapping(DictionaryActivity.this.IDSToPS, DictionaryActivity.this.PSToIDS, DictionaryActivity.this.PS_VALUES, DictionaryActivity.this.PS_IDS);
            if (z) {
                DictionaryActivity.this.databaseAccessor.loadWordFromFile(DictionaryActivity.this);
                DictionaryActivity.this.databaseAccessor.restoreFile(DictionaryActivity.this, DictionaryActivity.this.getDatabasePath(Constants.BACKUP_FILE_NAME));
            }
            return null;
        }

        protected void onPostExecute(String str) {
            try {
                DictionaryActivity.this.mainViewAnimator.setDisplayedChild(1);
                DictionaryActivity.this.addTabs(DictionaryActivity.TITLE_IDS);
                if (DictionaryActivity.this.sharedPreferences.getInt(Constants.KEY_LAST_SHOWN_POPUP_MESSAGE_ID, 0) != 5) {
                    DictionaryActivity.this.showInitialMessage(false);
                } else {
                    DictionaryActivity.this.manageAutoPopups();
                }
                DictionaryActivity.this.showSharedText(DictionaryActivity.this.getIntent());
                Utils.createShortCut(DictionaryActivity.this, DictionaryActivity.class);
                Utils.manageClipboardAndNotificationbar(DictionaryActivity.this);
                try {
                    DictionaryActivity.this.isTTSRequested = false;
                    Intent intent = new Intent();
                    intent.setAction("android.speech.tts.engine.CHECK_TTS_DATA");
                    DictionaryActivity.this.startActivityForResult(intent, DictionaryActivity.CHECK_TTS);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                DictionaryActivity.this.showHideOCR(true);
                DictionaryActivity.this.showOverlayPermissionPopupIfRequired();
            } catch (Exception e2) {
                if (!(e2 instanceof ActivityNotFoundException)) {
                    LogUtils.log(DictionaryActivity.this, e2);
                }
            }
            DictionaryActivity.this.isExitingOk = true;
            if (DictionaryActivity.this.IDSToPS.size() == 0) {
                DictionaryActivity.this.resetDatabase();
            }
        }
    }.execute();
}

当我在initialize方法和createTablesAndProcess行中调用this.wdb.execSQL("INSERT INTO type VALUES ( NULL,'MORE')");时,就会出现错误。如果您需要更多日志,请在评论中告诉我。

更新-Logcat:

2019-06-19 13:31:56.289 20450-20479/com.hdictionary.gu E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.hdictionary.gu, PID: 20450
java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:318)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:760)
 Caused by: android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: type.ps (code 2067)
    at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
    at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:734)
    at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
    at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677)
    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1606)
    at com.bappi.db.DatabaseAccessor.createTablesAndPorcess(DatabaseAccessor.java:152)
    at com.hdictionary.gu.DictionaryActivity$10.doInBackground(DictionaryActivity.java:1130)
    at com.hdictionary.gu.DictionaryActivity$10.doInBackground(DictionaryActivity.java:1100)
    at android.os.AsyncTask$2.call(AsyncTask.java:304)

1 个答案:

答案 0 :(得分:0)

这是一个简单的错误。表ps中列type的每个元素应该是唯一的,因为您是以这种方式创建表的。但是在执行语句INSERT INTO type VALUES ( NULL,'MORE')时,它已经在NULL列中具有相同的值(如果是第一列,则为ps)。