android - Crash in SQLite Insert "Column are not unique" -


i'm trying avoid duplicate data on database, made unique columns, , can insert them.

the problem when keep inserting same thing more 1 time, app crashes.. here logcat:

07-03 10:33:53.577: error/database(19974): failure 19 (columns suranumber, ayanumber not unique) on 0x2dc560 when executing 'insert favorite_sura (suranumber,ayanumber) values (17,1)' 07-03 10:33:54.440: error/androidruntime(19974): fatal exception: thread-11 07-03 10:33:54.440: error/androidruntime(19974): android.database.sqlite.sqliteconstraintexception: columns suranumber, ayanumber not unique: insert favorite_sura (suranumber,ayanumber) values (17,1) 07-03 10:33:54.440: error/androidruntime(19974):     @ android.database.sqlite.sqlitedatabase.native_execsql(native method) 07-03 10:33:54.440: error/androidruntime(19974):     @ android.database.sqlite.sqlitedatabase.execsql(sqlitedatabase.java:1763) 07-03 10:33:54.440: error/androidruntime(19974):     @ omar.quran1.quran$16.run(quran.java:910) 07-03 10:33:54.440: error/androidruntime(19974):     @ java.lang.thread.run(thread.java:1019) 

the code use insert, both suranumber , currentaya unique columns:

     new thread(new runnable() {                     public void run() {              /////////////// database             file dbfile = new file(munawwat_database_path);              if(!dbfile.exists())             {                 toast.maketext(mcontext, "error in db location", toast.length_short);             }             getstartpageaya();             sqlitedatabase db = sqlitedatabase.openorcreatedatabase(dbfile, null);             db.execsql("insert favorite_sura (suranumber,ayanumber) values ("+suranumber+","+currentaya+")");             db.close();                     }}).start(); 

why happening? thanks.

update1: sql used create table:

create table favorite_sura ( _id integer primary key, suranumber int, ayanumber int, unique (suranumber, ayanumber) ) 

you deliberately violating unique constraint? i'm not sure think should happening. catch obviously-named sqliteconstraintexception if application can afford ignore duplicates; usually, unique field unique reason, , raising exception duplicate necessary maintain application logical consistency.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

c# - SharpSVN - How to get the previous revision? -

php cli reading files and how to fix it? -