Working with Sqlite database in android -
if have databse file in .sql format, how can load in our android app & use data?
i giving complete code,plz reply if success
public class databasehelper extends sqliteopenhelper{ private context mycontext; private string db_path = "/data/data/gr.peos/databases/"; //private string db_path = mycontext.getapplicationcontext().getpackagename()+"/databases/"; private static string db_name = "blib.sqlite";//the extension may .sqlite or .db public sqlitedatabase mydatabase; /*private string db_path = "/data/data/" + mycontext.getapplicationcontext().getpackagename() + "/databases/";*/ public databasehelper(context context) throws ioexception { super(context,db_name,null,1); this.mycontext=context; boolean dbexist = checkdatabase(); if(dbexist) { //system.out.println("database exists"); opendatabase(); } else { system.out.println("database doesn't exist"); createdatabase(); } } public void createdatabase() throws ioexception{ boolean dbexist = checkdatabase(); if(dbexist) { //system.out.println(" database exists."); } else{ this.getreadabledatabase(); try{ copydatabase(); } catch(ioexception e){ throw new error("error copying database"); } } } private boolean checkdatabase() { //sqlitedatabase checkdb = null; boolean checkdb = false; try{ string mypath = db_path + db_name; file dbfile = new file(mypath); //checkdb = sqlitedatabase.opendatabase(mypath,null,sqlitedatabase.open_readwrite); checkdb = dbfile.exists(); } catch(sqliteexception e){ system.out.println("database doesn't exist"); } return checkdb; } 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("/data/data/gr.peos/databases/blib.sqlite"); // transfer byte 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); } public synchronized void close(){ if(mydatabase != null){ mydatabase.close(); } super.close(); } //your methods insert view contents of database
Comments
Post a Comment