android - Do I need to call getWritableDatabase() everytime I manipulate data -
i have newbie question sqlite databases in android:
do need retrieve writeable database everytime manipulate data?
so can write dao this:
class dao { private final sqlitedatabase database; public dao(sqliteopenhelper databasehelper){ database = databasehelper.getwritabledatabase(); } public void insert(...){ contentvalues cv = new contentvalues(4); database.insertorthrow(table, null, cv); ... } public void update(...){ contentvalues cv = new contentvalues(4); database.update(....); } }
or must write dao this:
class dao { private final sqliteopenhelper databasehelper; public dao(sqliteopenhelper databasehelper){ this.databasehelper = databasehelper } public void insert(...){ sqlitedatabase database = databasehelper.getwritabledatabase(); contentvalues cv = new contentvalues(4); database.insertorthrow(table, null, cv); ... } public void update(...){ sqlitedatabase database = databasehelper.getwritabledatabase(); contentvalues cv = new contentvalues(4); database.update(....); } }
if second approach correct one: need close database after every operation:
public void update(...){ sqlitedatabase database = databasehelper.getwritabledatabase(); contentvalues cv = new contentvalues(4); database.update(....); database.close(); }
i guess same databasehelper.getreadabledatabase() right?
you should close every cursor open. database instance doesn't need closed after each use.
i fetch writable database in oncreate()
, store resulting sqlitedatabase
member variable each activity, , never explicitly close instance. (but again, close every cursor i'm finished processing results.)
see managing sqlite connections in android little more discussion on subject.
Comments
Post a Comment