2

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)

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 + "))";

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 !

Thanks in advance

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

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

7
  • "why am I getting the error when I add something??" What are you trying to add? Is it a duplicate?
    – Sam
    Feb 11, 2013 at 18:23
  • It can't be a duplicate if it is empty. Feb 11, 2013 at 18:27
  • What are you adding: one row, multiple rows? (Post your onUpgrade() method.) If you are deleting the database why is onUpgrade() even being called? Logic stands that onCreate() should be called. Please post the full stacktrace.
    – Sam
    Feb 11, 2013 at 18:41
  • Ok - I added the database.delete after Feb 11, 2013 at 18:43
  • 1
    I cannot help you unless you post the code where you are trying to insert the words into your table... Also your Logcat mentions nothing about onUpgrade(), the problem is between ImportVocabList and addWord(). Post this code.
    – Sam
    Feb 11, 2013 at 18:53

3 Answers 3

3

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)";
0

name and foreign_word make up your primary (Unique!) key in this table. you must be trying to insert duplicated keyed rows. The combination of the two fields must be unique.

16
  • sure...you don't have any other constraints applied to the table do you?
    – David M
    Feb 11, 2013 at 18:29
  • What do you mean by constraints? (I'm an SQL Noob) All I have on the table is that String in the op Feb 11, 2013 at 18:30
  • any constraint on the table's data (Primary Key constrains that column(s) to be unique, you can constrain columns to contain only certain data (like Yes or No)
    – David M
    Feb 11, 2013 at 18:32
  • Nope. All I have defined is what I have is in that string Feb 11, 2013 at 18:33
  • a Constraint error is data related. I suspect your CREATE_TABLE_xxx worked, and you are loading data (can you throw in a Log.d() in there?)
    – David M
    Feb 11, 2013 at 18:35
0

Use

db.insertOrThrow(TABLE_VOCAB_WORDS, null, cv);

It worked for me for reasons I ignore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.