Best practices for Android databases -
my android application needs local database. best manner this? class sub use, subclass, reimplement, etc. have found information on net still don't know best practices.
it's pretty broad question , depends little on level on experience , usage.
but common practice create own contentprovider
abstract access database. way can use uri
execute select/update/delete/insert queries.
for sqlite database itself, use sqliteopenhelper
abstract creation , upgrading of sqlite database. allow upgrade database without user losing of data in easy manner.
i can attach piece of code used in 1 of older projects started. implementing whole thing may out of scope of single question/answer.
public class myserviceprovider extends contentprovider { private sqlitedatabase db; @override public boolean oncreate() { // initialize database , assign private variable mydatabasehelper sqlhelper = new mydatabasehelper(getcontext()); db = sqlhelper.getreadabledatabase(); return (db == null)?false:true; } @override cursor query (uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) { // handle query here, form it, checks , access db db.query(....); } } class mydatabasehelper extends sqliteopenhelper { private static final string log_tag = "myapptag"; private static final string db_name = "databasename"; private static final string table_name = "tablename"; private static final int database_version = 2; public static myserviceprovider.content_uri = uri.parse("content://com.mycompany.myapp.myappservice/mytableoridentifier"); public mydatabasehelper(context context){ super(context, db_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { string sql = "create table if not exists ...."; db.execsql(sql); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // here can perform updates when database structure changes // begin transaction db.begintransaction(); try { if(oldversion<2){ // upgrade database structure version 1 2 string altertable = "alter ...."; db.execsql(altertable); log.i(log_tag,"successfully upgraded version 2"); } // allows upgrade version next // recent 1 in multiple steps don't know if user has // skipped of previous updates if(oldversion<3){ // upgrade database structure version 2 3 string altertable = "alter ...."; db.execsql(altertable); log.i(log_tag,"successfully upgraded version 3"); } // when code executed, changes applied // database db.settransactionsuccessful(); } catch(exception ex){ ex.printstacktrace(); } { // ends transaction // if there error, database won't altered db.endtransaction(); } } }
and can use cursors interact database.
contentresolver contentresolver = getcontentresolver(); ... cursor c = contentresolver.query( // uri results in content://com.mycompany.myapp.myappservice/mytableoridentifier/someid uri.withappendedpath(myserviceprovider.content_uri, someid), new string[] { // fields need! "mydbfieldineed" }, null, null, null);
this return cursor
can iterate through , results. how things in android implemented (i.e. obtaining address address book works via uri , cursor too).
edit: realized links hard see code highlights. here link of important classes need.
- contentprovider | android developers
- sqliteopenhelper | android developers
- cursor | android developers
- urimatcher | android developers
edit 2: if work multiple tables, urimatcher
important source