sqlite - Android Database creation error. -
i have written create database code;i error/androidruntime(7193): @ com.android.xont.db.databasehelper.createdatabase(databasehelper.java:43)
this code :
public class databasehelper extends sqliteopenhelper { private static string db_path = "/data/data/com.android.xont.controller/databases/"; private static string db_name = "demouserventura_hema.db"; private sqlitedatabase demouserventura_hema; private final context mycontext; private databasehelper mydbhelper; public databasehelper(context context) { super(context, db_name, null, 1); this.mycontext = context; } /** * creates empty database on system , rewrites own * database. **/ public void createdatabase() throws ioexception { boolean dbexist = checkdatabase(); if (dbexist) { } else { this.getreadabledatabase().close(); //this.getreadabledatabase().getpath(); //system.out.println( " ===== " + this.getreadabledatabase().getpath()); try { copydatabase(); } catch (ioexception e) { throw new error(e); } } } private boolean checkdatabase() { sqlitedatabase checkdb = null; try { string mypath = db_path + db_name; checkdb = sqlitedatabase.opendatabase(mypath, null,sqlitedatabase.open_readonly); } catch (sqliteexception e) { // database does't exist yet. } if (checkdb != null) { checkdb.close(); } return 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; demouserventura_hema = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly); } @override public synchronized void close() { if (demouserventura_hema != null) demouserventura_hema.close(); super.close(); } @override public void oncreate(sqlitedatabase db) { } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } }
when call activity exception. database size : 4.55mb.
when print getreadabledatabase().getpath() return db path correctly.
please me out. i've been pulling hair out 1 week.
when run through emulator, copy small amount of file. not all. did manual way copy.
now problem htc phone. how fix exception. please me.
thanks in advance..
the assetmanager has file size limitations. on devices asset files larger 1mb cannot opened. have split file in smaller chunks, put in asset folder , reassemble chunks when app running.
see this question on how reassemble chunks.
to create chunks on osx or linux can use commandline tool 'split'.
see here or here or use google keywords 'android databasehelper asset' find tutorials on how implement databasehelper reassembles chunks asset folder when db openend first time.
Comments
Post a Comment