Android的SQLite的列

Android的SQLite的列

本文介绍了Android的SQLite的列.....不是唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android上使用SQLite和我得到这个错误

I am using SQLite on android and am getting this error

02-11 18:05:37.224: E/SQLiteDatabase(26583): android.database.sqlite.SQLiteConstraintException: columns name, foreign_word are not unique (code 19)

然而,这onUpgrade后立即被称为()被调用,所以应该有没有在数据库本身。这是所使用的SQL语句:

However, this is called right after onUpgrade() is called, so there should be nothing in the database itself. This is the SQL Statement used :

private static final String CREATE_DB_TB_2 = "CREATE TABLE "
        + TABLE_VOCAB_WORDS + "(" + COLUMN_NAME + " TEXT NOT NULL,"
        + COLUMN_WORD_FOREIGN + " TEXT NOT NULL," + COLUMN_WORD_ENGLISH
        + " TEXT NOT NULL," + COLUMN_CORRECT
        + " BYTE NOT NULL, PRIMARY KEY (" + COLUMN_NAME + ","
        + COLUMN_WORD_FOREIGN + "))";

由于没有什么数据库,我删除表,为什么我得到错误,当我添加的东西?此外,我打电话context.deleteDatabase(名);我每次运行应用程序。事情必须是独一无二的,因为表是空的!

As there is nothing in the database as I drop the table, why am I getting the error when I add something?? Furthermore, I am calling context.deleteDatabase(NAME); every time I run the app. The thing must be unique as the table is empty !

在此先感谢

全部LogCat中:

02-11 18:38:02.811: E/SQLiteDatabase(28631): Error inserting correct=0 english_meaning= tener prisa foreign_word=to be in a hurry  name=AQA GCSE Spanish Higher
02-11 18:38:02.811: E/SQLiteDatabase(28631): android.database.sqlite.SQLiteConstraintException: columns name, foreign_word are not unique (code 19)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1525)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1395)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at com.sharma.gcsevocabtester.DatabaseManipulator.addWord(DatabaseManipulator.java:168)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at com.sharma.gcsevocabtester.DatabaseManipulator$ImportVocabList.onPostExecute(DatabaseManipulator.java:420)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at com.sharma.gcsevocabtester.DatabaseManipulator$ImportVocabList.onPostExecute(DatabaseManipulator.java:1)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at android.os.AsyncTask.finish(AsyncTask.java:631)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at android.os.Looper.loop(Looper.java:155)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at android.app.ActivityThread.main(ActivityThread.java:5454)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at java.lang.reflect.Method.invokeNative(Native Method)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at java.lang.reflect.Method.invoke(Method.java:511)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
02-11 18:38:02.811: E/SQLiteDatabase(28631):    at dalvik.system.NativeStart.main(Native Method)

此外,我不知道这是否会有所作为,但我也有使用此字符串创建的另一个表。

Furthermore, I'm not sure if this will make a difference but I also have another table which is created using this String.

private static final String CREATE_DB_TB_1 = "CREATE TABLE "
        + TABLE_VOCAB_INFO + "(" + COLUMN_NAME
        + " TEXT NOT NULL PRIMARY KEY," + COLUMN_URL + " TEXT NOT NULL,"
        + COLUMN_DOWNLOADED + " TEXT NOT NULL)";

ImportVocabList是AsyncTask的。这是onPostExecute:

ImportVocabList is an Asynctask. This is the onPostExecute :

**
     * Method called after execution has happen, passing in the result
     *
     * @Override
     */
    protected void onPostExecute(ArrayList<Word> result) {
        super.onPostExecute(result);
        progressDialog.dismiss();
        boolean b = false;
        open();
        for (Word w : result) {

            addWord(w, vlName);
        }
        if (getAllVocabListsWhere(DatabaseManipulator.COLUMN_DOWNLOADED,
                "false").size() == 0) {
            b = true;
        }
        close();
        if (b) {
            context.finish();
        }
    }

的方法addWord如下:

The method addWord is as follows :

          public boolean addWord(Word w, String name) {
    try {
        ContentValues cv = new ContentValues();
        cv.put(COLUMN_NAME, name);
        cv.put(COLUMN_WORD_FOREIGN, w.getWordForeign());
        cv.put(COLUMN_WORD_ENGLISH, w.getWordEnglish());
        cv.put(COLUMN_CORRECT, w.getCorrect());
        database.insert(TABLE_VOCAB_WORDS, null, cv);
        return true;
    } catch (SQLiteException e) {
        System.out.println("SQL ERROR");
        return false;
    }
}

该问题已得到修复 - 有重复的条目,但他们被忽略了。谢谢

The problem has been fixed - there were duplicate entries but they were ignored. Thanks

推荐答案

看着你的AsyncTask的 onPostExecute()的方法,我相信,你至少有一个重复的组在结果值。只要从源头清除这些重复的。

Looking at your AsyncTask's onPostExecute() method, I am confident that your have at least one duplicate set of values in result. Simply purge these duplicates from your source.

或者你可以忽略重复的条目行,并通过进行:

Or you can tell your table to ignore the rows with duplicate entries and carry on via a "conflict-clause":

private static final String CREATE_DB_TB_2 = "CREATE TABLE "
        + TABLE_VOCAB_WORDS + "(" + COLUMN_NAME + " TEXT NOT NULL,"
        + COLUMN_WORD_FOREIGN + " TEXT NOT NULL," + COLUMN_WORD_ENGLISH
        + " TEXT NOT NULL," + COLUMN_CORRECT
        + " BYTE NOT NULL, PRIMARY KEY (" + COLUMN_NAME + ","
        + COLUMN_WORD_FOREIGN + ") ON CONFLICT IGNORE)";

这篇关于Android的SQLite的列.....不是唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 16:36