android - Duplicate rows in sqlite database -
i have android app local sqlite database have problem duplicate row. every time when start app, addding items db item same id exist there. problem cannot update/rewrite rows has same id. db: link
declarations:
// database name private static final string database_name = "foodsdb"; // contacts table name private static final string table_foods = "tablefoods"; // contacts table columns names private static final string key_id = "id"; private static final string key_foodname = "food_name"; private static final string key_alergen_no = "alergen_numbers"; private static final string key_date = "date"; private static final string key_food_number = "food_number";
db create table:
// creating table @override public void oncreate(sqlitedatabase db) { string create_foods_table = "create table " + table_foods + "(" + key_id + " text," + key_foodname + " text," + key_alergen_no + " text," + key_date + " text," + key_food_number + " integer"+")"; db.execsql(create_foods_table); }
db insert:
// adding new food void addfood(foodsdb food) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_id, food.getid()); values.put(key_foodname, food.getfoodname()); values.put(key_alergen_no, food.getalergenynumbers()); values.put(key_date,food.getdate()); values.put(key_food_number,food.getfoodnumber()); db.begintransaction(); try { long rowid = db.insertorthrow(table_foods, null, values); if (rowid == -1) { // insertion failed string whereclause = key_date; //string[] whereargs = new string[] {values.getasstring(key_date)}; db.update(table_foods, values, whereclause, null); } db.settransactionsuccessful(); } { db.endtransaction(); } db.close(); // closing database connection }
any idea why have more rows same id in db? try set id primary key not work.
Comments
Post a Comment