Android database using ORMLITE sqlite -
i have used sqlite database using ormlite.i have declared primary key in database follows:in database fetch values , display in listview. when delete entry.in list view..primary key goes on increasing values.
@databasefield(generatedid = true,columnname = "teacher_id") int teacherid; try { toast.maketext(detaillistview.this, "delete entry", toast.length_long).show(); deletebuilder<teacherdetails, integer> deletebuilder=teacherdao.deletebuilder(); deletebuilder.where().eq("teacher_address", set.teacheraddress); deletebuilder.delete(); stud_list = teacherdao.queryforall(); //adapter = new usersadapter(detaillistview.this, stud_list); // list.setadapter(adapter); list.invalidate(); teacherdao.executeraw("delete sqlite_sequence name='teacher_details';"); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); }
//code creating db.
@override public void oncreate (sqlitedatabase sqlitedatabase, connectionsource connectionsource) { try { tableutils.createtable(connectionsource, teacherdetails.class); tableutils.createtable(connectionsource, studentdetails.class); } catch (sqlexception e) { log.e(databasehelper.class.getname(), "unable create datbases", e); } } @override public void onupgrade(sqlitedatabase sqlitedatabase, connectionsource connectionsource, int oldver, int newver) { try { tableutils.droptable(connectionsource, teacherdetails.class, true); tableutils.droptable(connectionsource, studentdetails.class, true); oncreate(sqlitedatabase, connectionsource); } catch (sqlexception e) { log.e(databasehelper.class.getname(), "unable upgrade database version " + oldver + " new " + newver, e); } }
for example before delete 1)primary key :1 2)primary key:2(if delete values) 3)primary key:3
after delete 1)primary key:1 2)primary key:3(not start primary key value 2) 3)primary key 4
this requirement
this how database sequences supposed work. example simple, imagine table 10000000 entries. imagine them being ids first 10000000 natural numbers. imagine person goes , deletes 345346th, 123344th , 234545th. how should know ids vacant, without requiring huge number of calculations.
if case specific , have few entries, need sequential, have give on autoincrements , code yourself. please, consider corner cases, because beleive quite few.
Comments
Post a Comment