i trying insert values in table. there 1 value inserted. getting error in log cat when trying insert new values.
log cat shows :
abort @ 13 in [insert event(totalminutesfrom,dayofweek,title,location,totalminutesto,id) values (?,?,?,?,?,?)]: unique constraint failed: event.id 01-24 11:34:39.764 7763-7763/com.example.siddhi.timetablelayout e/sqlitedatabase: error inserting totalminutesfrom=694 dayofweek=null title=qxs location=eded & mariz totalminutesto=0 id=0 01-24 11:34:39.764 7763-7763/com.example.siddhi.timetablelayout e/sqlitedatabase: android.database.sqlite.sqliteconstraintexception: unique constraint failed: event.id (code 1555) 01-24 11:34:39.764 7763-7763/com.example.siddhi.timetablelayout e/sqlitedatabase: @ android.database.sqlite.sqliteconnection.nativeexecuteforlastinsertedrowid(native method) 01-24 11:34:39.764 7763-7763/com.example.siddhi.timetablelayout e/sqlitedatabase: @ android.database.sqlite.sqliteconnection.executeforlastinsertedrowid(sqliteconnection.java:782) 01-24 11:34:39.764 7763-7763/com.example.siddhi.timetablelayout e/sqlitedatabase: @ android.database.sqlite.sqlitesession.executeforlastinsertedrowid(sqlitesession.java:788) 01-24 11:34:39.764 7763-7763/com.example.siddhi.timetablelayout e/sqlitedatabase: @ android.database.sqlite.sqlitestatement.executeinsert(sqlitestatement.java:86) 01-24 11:34:39.764 7763-7763/com.example.siddhi.timetablelayout e/sqlitedatabase: @ android.database.sqlite.sqlitedatabase.insertwithonconflict(sqlitedatabase.java:1471) 01-24 11:34:39.764 7763-7763/com.example.siddhi.timetablelayout e/sqlitedatabase: @ android.database.sqlite.sqlitedatabase.insert(sqlitedatabase.java:1341) 01-24 11:34:39.764 7763-7763/com.example.siddhi.timetablelayout e/sqlitedatabase: @ com.example.siddhi.timetablelayout.eventtablehelper.addevent(eventtablehelper.java:76) 01-24 11:34:39.764 7763-7763/com.example.siddhi.timetablelayout e/sqlitedatabase: @ com.example.siddhi.timetablelayout.addeventactivity$5.onclick(addeventactivity.java:217)
its showing error on these 2 lines while inserting row.
db.insert(table, null, values); db.addevent(new eventdata(eventtitle,dayofweek,totalminutesfrom, totalminutesto,location));
eventtablehelper
public class eventtablehelper extends sqliteopenhelper { private static final string table = "event"; private static final string key_id = "id"; private static final string key_title = "title"; private static final string key_location = "location"; private static final string key_day_of_week = "dayofweek"; private static final string key_total_minutes_from = "totalminutesfrom"; private static final string key_total_minutes_to = "totalminutesto"; public eventtablehelper(context context) { super(context, constants.database_name, null, constants.database_version); //3rd argument passed cursorfactory instance } // creating tables @override public void oncreate(sqlitedatabase db) { //createtable(db); } public void createtable(sqlitedatabase db){ string create_events_table = "create table " + table + "(" + key_id + " integer primary key," + key_title + " text," + key_day_of_week +"text" + key_total_minutes_from +"integer" + key_total_minutes_to + "integer" + key_location + "text" + ")"; db.execsql(create_events_table); } // upgrading database @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // drop older table if existed // db.execsql("drop table if exists " + table); // createtable(db); // create tables again //oncreate(db); } // code add new contact public void addevent(eventdata event) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_id, event.getid()); values.put(key_title,event.gettitle()); // contact name values.put(key_day_of_week,event.getdayofweek()); values.put(key_total_minutes_from,event.getfromminutes()); values.put(key_total_minutes_to,event.gettominutes()); values.put(key_location,event.getlocation()); // inserting row db.insert(table, null, values); //2nd argument string containing nullcolumnhack db.close(); // closing database connection } // code single contact eventdata getevent(int id) { sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.query(table, new string[] { key_id, key_title, key_day_of_week, key_total_minutes_from,key_total_minutes_to,key_location }, key_id + "=?", new string[] { string.valueof(id) }, null, null, null, null); if (cursor != null) cursor.movetofirst(); eventdata eventdata = new eventdata(integer.parseint(cursor.getstring(0)),cursor.getstring(1), cursor.getstring(2), cursor.getint(3),cursor.getint(4),cursor.getstring(5)); return eventdata; } // code contacts in list view public list<eventdata> getallevents() { list<eventdata> conlist = new arraylist<eventdata>(); // select query string selectquery = "select * " + table; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { eventdata event = new eventdata(); event.setid(integer.parseint(cursor.getstring(0))); event.settitle(cursor.getstring(1)); event.setdayofweek(cursor.getstring(2)); event.setfromminutes(cursor.getint(3)); event.settominutes(cursor.getint(4)); event.setlocation(cursor.getstring(5)); // adding contact list conlist.add(event); } while (cursor.movetonext()); } // return contact list return conlist; } }
how solve this??
your code violates primary key's uniqueness constraint on key_id
field.
two possible solutions are:
- make sure
eventdata.getid()
returns unique values per object. now, don't see pass identifier constructor , perhaps events inserted sameid
value. - if don't care generating ids yourself, can add
autoincrement
settingkey_id
column definition. waykey_id
field filled automatically , each row have own, unique value. once there, don't forget remove addingkey_id
contentvalues
yourself.
Comments
Post a Comment