i want sqlite database instance wiped away when program starts.
what tried make method on class mydbadapter.java this:
public class mydbadapter { private static final string tag = "notesdbadapter"; private databasehelper mdbhelper; private sqlitedatabase mdb; private static final string database_name = "gpslocdb"; private static final string permission_table_create = "create table permission ( fk_email1 varchar, fk_email2 varchar, validated tinyint, hour1 time default '08:00:00', hour2 time default '20:00:00', date1 date, date2 date, weekend tinyint default '0', fk_type varchar, primary key (fk_email1,fk_email2))"; private static final string user_table_create = "create table user ( email varchar, password varchar, fullname varchar, mobilephone varchar, mobileoperatingsystem varchar, primary key (email))"; private static final int database_version = 2; private final context mctx; private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql(permission_table_create); db.execsql(user_table_create); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists user"); db.execsql("drop table if exists permission"); oncreate(db); } } /** * constructor - takes context allow database * opened/created * * @param ctx context within work */ public mydbadapter(context ctx) { this.mctx = ctx; } /** * open database. if cannot opened, try create new * instance of database. if cannot created, throw exception * signal failure * * @return (self reference, allowing chained in * initialization call) * @throws sqlexception if database neither opened or created */ public mydbadapter open() throws sqlexception { mdbhelper = new databasehelper(mctx); mdb = mdbhelper.getwritabledatabase(); return this; } public void close() { mdbhelper.close(); } public long createuser(string email, string password, string fullname, string mobilephone, string mobileoperatingsystem) { contentvalues initialvalues = new contentvalues(); initialvalues.put("email",email); initialvalues.put("password",password); initialvalues.put("fullname",fullname); initialvalues.put("mobilephone",mobilephone); initialvalues.put("mobileoperatingsystem",mobileoperatingsystem); return mdb.insert("user", null, initialvalues); } public cursor fetchallusers() { return mdb.query("user", new string[] {"email", "password", "fullname", "mobilephone", "mobileoperatingsystem"}, null, null, null, null, null); } public cursor fetchuser(string email) throws sqlexception { cursor mcursor = mdb.query(true, "user", new string[] {"email", "password", "fullname", "mobilephone", "mobileoperatingsystem"} , "email" + "=" + email, null, null, null, null, null); if (mcursor != null) { mcursor.movetofirst(); } return mcursor; } public list<friend> retrieveallusers() { list <friend> friends=new arraylist<friend>(); cursor result=fetchallusers(); if( result.movetofirst() ){ do{ //note.getstring(note.getcolumnindexorthrow(notesdbadapter.key_title))); friends.add(new friend(result.getstring(result.getcolumnindexorthrow("email")), "","","","")); }while( result.movetonext() ); } return friends; } }
what best way this?
beside oncreate()
, onupgrade()
can override onopen()
. drop tables there , call oncreate()
.
public class myapplication extends application { protected static final string log_tag = "myapplication"; private static databaseadapter mdb; private static myapplication minstance; /** * @return instance of database adapter. */ public static databaseadapter getdatabaseadapter() { return mdb; } /** * @return instance of application. */ public static context getinstance() { return minstance; } @override public void oncreate() { super.oncreate(); log.w(log_tag, "application::oncreate"); minstance = this; mdb = new databaseadapter(); } @override public void onterminate() { // close internal db getdatabaseadapter().close(databaseadapter.internal); log.e(log_tag, "::onterminate::"); super.onterminate(); } }
the advantage of subclassing application called when application started or terminated. independent of activity started. global operations open/close database should placed here.
documentation:
base class need maintain global application state. can provide own implementation specifying name in androidmanifest.xml's tag, cause class instantiated when process application/package created.
Comments
Post a Comment