android - Data wipes out automatically in SQLlite -
i'm new android development , i'm pretty confused issue. have activity gets inputs user , inserts them sqllite database. here's code insert data table.
@override public void onclick(view v) { switch (v.getid()) { case r.id.savebtn: maincalander.db = this.openorcreatedatabase(maincalander.dbname, mode_private, null); system.out.println("db opened!!"); //set duplicate flag normal duplicateflag=false; //retrieve text edit text boxes string t1 = title.gettext().tostring(); string t2 = desc.gettext().tostring(); //make date string string datestring = maincalander.selectedyear + '-' + maincalander.selectedmonth + '-' + maincalander.selectedday; system.out.println(datestring); //time string string timestring = time.gettext().tostring(); cursor c1 = maincalander.db.rawquery("select * " + maincalander.tablename, null); if (c1 != null) { system.out.println("not null"); if (c1.movetofirst()){ system.out.println("moved first"); { system.out.println("doing"); string temptitle = c1.getstring(c1.getcolumnindex("event")); string tempdate = c1.getstring(c1.getcolumnindex("date")); system.out.println(datestring + " = " + tempdate); //check duplicate titles if (t1.equals(temptitle) && datestring.equals(tempdate)) { system.out.println(datestring + " = " + tempdate); toast.maketext(getapplicationcontext(), "event name exist!", toast.length_long).show(); duplicateflag=true; break; } } while (c1.movetonext()); } } //insert db if(!duplicateflag){ maincalander.db.begintransaction(); try { maincalander.db.execsql("insert "+ maincalander.tablename + " values ('"+ t1 + "','" + t2 + "','" + datestring + "','" + timestring + "');"); maincalander.db.settransactionsuccessful(); } catch (sqliteexception e) { e.printstacktrace(); maincalander.db.endtransaction(); } { maincalander.db.endtransaction(); } system.out.println("one row inserted successfully!"); toast.maketext(getapplicationcontext(), "event saved!", toast.length_long).show(); } maincalander.db.close(); this.finish(); break; default: break; } }
now, above code working fine (at first didn't know begintransaction(), settransactionsuccessful() & endtransaction(). added them trying resolve problem had). after inserting did calculations data without problem.
but @ 1 point tried moving different activity right after database insertion , there no problem. no errors, no exceptions, nothing. that's when realized of inserted data had been wiped clean database. pretty confused this.
at first thinking database commit automatically done when issue sql statement using execsql() method. searched in internet , did not find commit; keyword have in oracle. found stackoverflow threads on begintransaction(), settransactionsuccessful() & endtransaction() implied commit transaction database. tried also, still issue there.
i tried killing application , restating check whether there data loss when app life cycle destroyed, after inserting data database inserted. , did lose data. don't know what's happening here. appreciate if me on this.
here's main class creates tables,
public class maincalander extends activity implements view.onclicklistener{ final context context = this; public static calendarview calendar; public static sqlitedatabase db; public static final string dbname = "appointmentman"; public static final string tablename = "events"; //calendar public static string selectedyear; public static string selectedmonth; public static string selectedday; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main_calander); view createbtn = findviewbyid(r.id.createbtn); createbtn.setonclicklistener(this); view deletebtn = findviewbyid(r.id.deletebtn); deletebtn.setonclicklistener(this); view editbtn = findviewbyid(r.id.editbtn); editbtn.setonclicklistener(this); view movebtn = findviewbyid(r.id.movebtn); movebtn.setonclicklistener(this); initializecalendar(); try { //instantiate db object db = this.openorcreatedatabase(dbname, mode_private, null); //creates table main events table db.execsql("create table if not exists " + tablename +" (event varchar not null," + "description text not null," + "date date not null," + "time text);"); } catch (sqliteexception se ) { toast.maketext(getapplicationcontext(), "couldn't create or open database", toast.length_long).show(); } { if (db != null) { db.execsql("delete " + tablename); db.close(); } } } public void initializecalendar() { calendar = (calendarview) findviewbyid(r.id.calenderview); calendar.setshowweeknumber(false); calendar.setondatechangelistener(new ondatechangelistener() { @override public void onselecteddaychange(calendarview view, int year, int month, int day) { toast.maketext(getapplicationcontext(), day + "/" + month + "/" + year, toast.length_short).show(); selectedday = integer.tostring(day); selectedmonth = integer.tostring(month); selectedyear = integer.tostring(year); } } ); } @override public void onclick(view v) { switch (v.getid()) { case r.id.createbtn: intent intentnewap = new intent(this, newappointment.class); startactivity(intentnewap); break; case r.id.deletebtn: intent intentdel = new intent(this, deleteactivity.class); startactivity(intentdel); break; case r.id.editbtn: intent intentedit = new intent(this, showlistviewing.class); startactivity(intentedit); break; case r.id.movebtn: intent intentmove = new intent(this, showlistmoving.class); startactivity(intentmove); break; default: break; } }
}
it looks have extended sqlitedatabase. shouldn't this. should extend sqliteopenhelper instead.
see documentation on storage options more information.
this basic example shown there:
public class dictionaryopenhelper extends sqliteopenhelper { private static final int database_version = 2; private static final string dictionary_table_name = "dictionary"; private static final string dictionary_table_create = "create table " + dictionary_table_name + " (" + key_word + " text, " + key_definition + " text);"; dictionaryopenhelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql(dictionary_table_create); } }
Comments
Post a Comment