sqlite - Best practices for exposing multiple tables using content providers in Android -
i'm building app have table events , table venues. want able grant other applications access data. have few questions related best practices kind of problem.
how should structure database classes? have classes eventsdbadapter , venuesdbadapter, provide logic querying each table, while having separate dbmanager (extends sqliteopenhelper) managing database versions, creating/upgrading databases, giving access database (getwriteable/readeabledatabase). recommended solution, or better off either consolidating 1 class (ie. dbmanager) or separation , letting each adapter extends sqliteopenhelper?
how should design content providers multiple tables? extending previous question, should use 1 content provider whole app, or should create separate providers events , venues?
most examples find deal single table apps, appreciate pointers here.
it's bit late you, others may find useful.
first need create multiple content_uris
public static final uri content_uri1 = uri.parse("content://"+ provider_name + "/sampleuri1"); public static final uri content_uri2 = uri.parse("content://"+ provider_name + "/sampleuri2");
then expand uri matcher
private static final urimatcher urimatcher; static { urimatcher = new urimatcher(urimatcher.no_match); urimatcher.adduri(provider_name, "sampleuri1", sample1); urimatcher.adduri(provider_name, "sampleuri1/#", sample1_id); urimatcher.adduri(provider_name, "sampleuri2", sample2); urimatcher.adduri(provider_name, "sampleuri2/#", sample2_id); }
then create tables
private static final string database_name = "sample.db"; private static final string database_table1 = "sample1"; private static final string database_table2 = "sample2"; private static final int database_version = 1; private static final string database_create1 = "create table if not exists " + database_table1 + " (" + _id1 + " integer primary key autoincrement," + "data text, stuff text);"; private static final string database_create2 = "create table if not exists " + database_table2 + " (" + _id2 + " integer primary key autoincrement," + "data text, stuff text);";
don't forget add second database_create
oncreate()
you going use switch-case block determine table used. insert code
@override public uri insert(uri uri, contentvalues values) { uri _uri = null; switch (urimatcher.match(uri)){ case sample1: long _id1 = db.insert(database_table1, "", values); //---if added successfully--- if (_id1 > 0) { _uri = contenturis.withappendedid(content_uri1, _id1); getcontext().getcontentresolver().notifychange(_uri, null); } break; case sample2: long _id2 = db.insert(database_table2, "", values); //---if added successfully--- if (_id2 > 0) { _uri = contenturis.withappendedid(content_uri2, _id2); getcontext().getcontentresolver().notifychange(_uri, null); } break; default: throw new sqlexception("failed insert row " + uri); } return _uri; }
you need devide delete
, update
, gettype
, etc. wherever provider calls database_table or content_uri add case , have database_table1 or content_uri1 in 1 , #2 in next , on many want.
Comments
Post a Comment