android studio cant open .sqlite database file -
i'm building first app using android studio accesses external database. app crashing , giving errors saying database can not opened. i've followed videos on youtube still have problem.
when copy database .sqlite file desktop , paste assets folder error "file loaded in wrong encoding: utf-8". tried reloading file in several encodings same error message. i'm guessing issue.
any other tips or notes welcome , appreciated.
public class databasehelper extends sqliteopenhelper { //the android's default system path of application database. private static string db_path = "/data/data/invain3219/databases/"; private static string db_name = "partyprobedb.sqlite"; private sqlitedatabase mydatabase; private final context mycontext; /** * constructor * takes , keeps reference of passed context in order access application assets , resources. * @param context */ 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){ //do nothing - database exist }else{ //by calling method , empty database created default system path //of application gonna able overwrite database our database. this.getreadabledatabase(); try { copydatabase(); } catch (ioexception e) { throw new error("error copying database"); } } } /** * check if database exist avoid re-copying file each time open application. * @return true if exists, false if doesn't */ 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; } /** * copies database local assets-folder created empty database in * system folder, can accessed , handled. * done transfering bytestream. * */ 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_readonly); } @override public synchronized void close() { if(mydatabase != null) mydatabase.close(); super.close(); } @override public void oncreate(sqlitedatabase db) { } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } // add public helper methods access , content database. // return cursors doing "return mydatabase.query(....)" it'd easy // create adapters views. }
id int change below enough , don't need " " + tableinfo.info_user + " = '" + id + "';"
int id = curs.getint(0); curs = db.rawquery("select * " + tableinfo.table_user_info + " " + tableinfo.info_user + " = " + id, null);
Comments
Post a Comment