Tuesday 15 February 2011

android - localization of database -



android - localization of database -

in application making 2 different databases 2 different languages ,,both databases in assets folder, based on locale should take particular database. not sure how implement it, though set database name in string folder different languages , how pass name db_name not sure. know how localize database when have 2 different databases in assets folder. below code

public class matdatabase { protected static final string tag = "ability database"; protected databasehelper mdbhelper; protected sqlitedatabase mdb; protected static final string database_name = "ability.sqlite"; protected static final int database_version = 1; protected final context mctx; public class databasehelper extends sqliteopenhelper { // android's default scheme path of application database. private final string db_path = environment.getdatadirectory() + "/data/com.in.android.ability/databases/"; private final static string db_name = "ability.sqlite"; private sqlitedatabase mydatabase; private final context mycontext; public databasehelper(context context) { super(context, db_name, null, database_version); this.mycontext = context; //mycontext.getresources().getstring(r.string.dbname); } public void createdatabase() throws ioexception { boolean dbexist = checkdatabase(); if (dbexist) { // nil - database exist } else { this.getwritabledatabase(); seek { copydatabase(); } grab (ioexception e) { throw new error("error copying database"); } { this.close(); } } } private boolean checkdatabase() { sqlitedatabase checkdb = null; seek { string name = mycontext.getresources().getstring(r.string.dbname); string mypath = db_path + name; file dbfile = new file(mypath); homecoming dbfile.exists(); } grab (sqliteexception e) { // database does't exist yet. e.printstacktrace(); } if (checkdb != null) { checkdb.close(); } homecoming checkdb != null ? true : false; } private void copydatabase() throws ioexception { // open local db input stream inputstream myinput = mycontext.getassets().open(db_name); // path created empty db string outfilename = db_path + db_name; // open empty db output stream outputstream myoutput = new fileoutputstream(outfilename); // transfer bytes inputfile outputfile byte[] buffer = new byte[1024]; int length; while ((length = myinput.read(buffer)) > 0) { myoutput.write(buffer, 0, length); } // close streams myoutput.flush(); myoutput.close(); myinput.close(); } public void opendatabase() throws sqlexception { // open database string mypath = db_path + db_name; mydatabase = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readwrite); this.getwritabledatabase(); }}

android sqlite locale

No comments:

Post a Comment