Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite INSERT OR IGNORE doesn't work

Tags:

android

sqlite

Why do the IGNORE statement doesn't work? Everytime I run this the same value were insert in the database and I don't know why?

        myDB = this.openOrCreateDatabase(MY_DB_NAME, MODE_PRIVATE, null);
        myDB.execSQL("INSERT OR IGNORE INTO " + MY_DB_TABLE + "(db_datum, db_zahlen, db_scheine)" 
                +"VALUES ('"+datum+"',"+
                "'"+zahlen+"',"+
                "'"+9+"');");

db schema is:

  • db_datum (varchar(100))

  • db_zahlen (varchar(100))

  • db_scheine (integer)

I have the 2 String "datum" and "zahlen" and would like to insert them in the database. Now the problem is, if the same strings are already in the db and the code run again they sould be ignored and never insert again.

like image 857
Bubbi Avatar asked Aug 02 '11 20:08

Bubbi


1 Answers

You can use insertWithOnConflict. Ex:

mDb.insertWithOnConflict(DATABASE_TABLE, null, initialValues, SQLiteDatabase.CONFLICT_IGNORE);
like image 173
Gautham Avatar answered Sep 19 '22 14:09

Gautham