the following different files 1.mainactivity.java
package com.example.ganesha.myapplication; import android.content.context; import android.content.sharedpreferences; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.button; import android.widget.cursoradapter; import android.widget.edittext; import android.widget.spinner; import android.widget.toast; import java.io.ioexception; import java.util.calendar; import java.util.locale; public class mainactivity extends appcompatactivity { edittext studentname, studentrollno; //string selectedmess; context context = this; cursor c = null; public static final string mypref = "mypreference"; public static final string name = "studentname"; public static final string rollno = "studentrollno"; public static final string messname = "studentselectedmess"; sharedpreferences sharedpreferences; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button b1; b1 = (button) findviewbyid(r.id.button); sharedpreferences = getsharedpreferences(mypref, context.mode_private); final spinner spinner = (spinner) findviewbyid(r.id.spinner); arrayadapter<charsequence> adapter; studentname = (edittext) findviewbyid(r.id.name); studentrollno = (edittext) findviewbyid(r.id.rollno); adapter = arrayadapter.createfromresource(this, r.array.mess, android.r.layout.simple_spinner_item); adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); ((button) findviewbyid(r.id.button)).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { hostelierdbhelper hostelierdbhelper = new hostelierdbhelper(mainactivity.this); try { hostelierdbhelper.create(); } catch (ioexception ioe) { throw new error("unable create database"); } try { hostelierdbhelper.open(); } catch (sqlexception sqle) { throw sqle; } toast.maketext(mainactivity.this, "success", toast.length_short).show(); calendar scalendar = calendar.getinstance(); string daylongname = scalendar.getdisplayname(calendar.day_of_week, calendar.long, locale.getdefault()); string m = "thursday"; int res = daylongname.compareto(m); if (res == 0) { c = hostelierdbhelper.query("kailash",m,null, null, null, null, null); if (c.movetofirst()) { { toast.maketext(mainactivity.this, "breakfast " + c.getstring(1) + "\n" + "lunch: " + c.getstring(2) + "\n" + "snacks: " + c.getstring(3) + "\n" + "dinner: " + c.getstring(4), toast.length_long).show(); } while (c.movetonext()); } } } }); }
}
2.hostelierdbhelper package com.example.ganesha.myapplication;
import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteexception; import android.database.sqlite.sqliteopenhelper; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.text.simpledateformat; import java.util.calendar; import java.util.locale; public class hostelierdbhelper extends sqliteopenhelper { private static string db_path = ""; private static string db_name = "messmenus"; private static string table_kailash = "kailash"; cursor cursor; //private static string table_employee = "employee"; //private static string table_phone = "employeephone"; private final context context; private sqlitedatabase db; public hostelierdbhelper(context context) { super(context, db_name, null, 1); if (android.os.build.version.sdk_int >= 17) { db_path = context.getapplicationinfo().datadir + "/databases/"; } else { db_path = "/data/data/" + context.getpackagename() + "/databases/"; } this.context = context; } // creates empty database on system , rewrites own database. public void create() throws ioexception { boolean dbexist = checkdatabase(); if (dbexist) { //do nothing - database exist } else { // 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-copy data private boolean checkdatabase() { sqlitedatabase checkdb = null; try { string path = db_path + db_name; checkdb = sqlitedatabase.opendatabase(path, null, sqlitedatabase.open_readonly); } catch (sqliteexception e) { // database don't exist yet. e.printstacktrace(); } if (checkdb != null) { checkdb.close(); } return checkdb != null ? true : false; } // copy assets db private void copydatabase() throws ioexception { //open local db input stream inputstream myinput = context.getassets().open(db_name); 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); } myoutput.flush(); myoutput.close(); myinput.close(); } //open database public boolean open() { try { string mypath = db_path + db_name; db = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readwrite); return true; } catch (sqlexception sqle) { db = null; return false; } } @override public synchronized void close() { if (db != null) db.close(); super.close(); } @override public void oncreate(sqlitedatabase db) { } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } // calendar scalendar=calendar.getinstance(); // string daylongname = scalendar.getdisplayname(calendar.day_of_week, calendar.long, locale.getdefault()); // string m="monday"; // int res=daylongname.compareto(m); // if(res == 0){ public cursor query(string table, string columns, string selection, string[] selectionargs, string groupby, string having, string orderby) { return db.query("kailash", new string[]{columns},null, null, null, null, null); }
} following error logsheet
07-21 20:46:10.602 8141-8189/com.example.ganesha.myapplication i/openglrenderer: initialized egl, version 1.4 07-21 20:46:10.637 8141-8189/com.example.ganesha.myapplication w/egl_emulation: eglsurfaceattrib not implemented 07-21 20:46:10.637 8141-8189/com.example.ganesha.myapplication w/openglrenderer: failed set egl_swap_behavior on surface 0xad154700, error=egl_success 07-21 20:46:19.300 8141-8141/com.example.ganesha.myapplication e/sqlitelog: (1) no such table: kailash 07-21 20:46:19.304 8141-8141/com.example.ganesha.myapplication d/androidruntime: shutting down vm 07-21 20:46:19.306 8141-8141/com.example.ganesha.myapplication e/androidruntime: fatal exception: main process: com.example.ganesha.myapplication, pid: 8141 android.database.sqlite.sqliteexception: no such table: kailash (code 1): , while compiling: select thursday kailash @ android.database.sqlite.sqliteconnection.nativepreparestatement(native method) @ android.database.sqlite.sqliteconnection.acquirepreparedstatement(sqliteconnection.java:887) @ android.database.sqlite.sqliteconnection.prepare(sqliteconnection.java:498) @ android.database.sqlite.sqlitesession.prepare(sqlitesession.java:588) @ android.database.sqlite.sqliteprogram.<init>(sqliteprogram.java:58) @ android.database.sqlite.sqlitequery.<init>(sqlitequery.java:37) @ android.database.sqlite.sqlitedirectcursordriver.query(sqlitedirectcursordriver.java:44) @ android.database.sqlite.sqlitedatabase.rawquerywithfactory(sqlitedatabase.java:1316) @ android.database.sqlite.sqlitedatabase.querywithfactory(sqlitedatabase.java:1163) @ android.database.sqlite.sqlitedatabase.query(sqlitedatabase.java:1034) @ android.database.sqlite.sqlitedatabase.query(sqlitedatabase.java:1202) @ com.example.ganesha.myapplication.hostelierdbhelper.query(hostelierdbhelper.java:137) @ com.example.ganesha.myapplication.mainactivity$1.onclick(mainactivity.java:83) @ android.view.view.performclick(view.java:5198) @ android.view.view$performclick.run(view.java:21147) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616)
why error come when there database , tabledatabase image
easiest 1 without controlling versions is,
private static final string drop_table = "drop table if exists " + table_name; private static final string create_table = "create table " + table_name + " ( " + [your field definitions here] + " )"; @override public void oncreate(sqlitedatabase db) { db.execsql(create_table); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql(drop_table); db.execsql(create_table); } @override public void ondowngrade(sqlitedatabase db, int oldversion, int newversion) { onupgrade(db, oldversion, newversion); }
your problem in stacktrace querying db without creating it.
Comments
Post a Comment