android - SQLLITEDATABASE crash-oncreate not called ? db.getreadabledatabase/getwritabledatabase crash -
i'm beginner @ android , sqlite well. have been trying build database retrieves , shows entries items, price , time. have looked through other topics can't seem find why crashes. have tried lot of ways make work, code trying use:
public class data extends sqliteopenhelper { private static final string database_name ="items.db"; private static final string t="database created"; private static final int database_version=1; context ct; sqlitedatabase db; public data (context ctx) { super(ctx, database_name, null, database_version); ct=ctx; } @override public void oncreate(sqlitedatabase db) { db.execsql("create table record (_id primary key autoincrement,time text,item text,type text,amt text)"); int duration = toast.length_long; toast toast = toast.maketext(ct, t, duration); toast.show(); } @override public void onupgrade(sqlitedatabase db,int oldversion,int newversion) { db.execsql("drop table if exists"+table_name); oncreate(db); } }
here class use run data class object
public class mainscreen extends activity { private data data; private static string[] cols={"_id","time","item","type","amt"}; private static string order_by = time + "desc"; private static int[] to={r.id.textview5,r.id.textview3,r.id.textview,r.id.textview4,r.id.textview2}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.mainscreen); data = new data(this); } public void tryit(view view) { time t=new time(); data test =new data(this); add(t.tostring(),"tomato", "200", "food"); // cursor cursor = getd(); // show(cursor); } private void add(string t,string item, string price,string type) { sqlitedatabase db = data.getwritabledatabase(); db.execsql("insert "+table_name+" values(null,'" + t + "','" + item + "','" + type + "','" + price + "')"); } }
as of i'm trying database show entries add using add();
- it not seem run
oncreate
@ data class (i used toast try , see ifoncreate
runs @ all) - trying use
sqllitedatabase
objectgetreadabledatabase/getwritabledatabase
causes crash - even simple
execsql("create******")
method above causing crash. - the worst part worked 2-3 times yesterday. showed me 5-8 entries using
listview
wanted, , crashing sob.
i cant seem find problem!
i'm not sure how stackdump/trace
if needed please tell me how it. have marked out cursor/adapters there no point getting work if database doesn't work.
i use tryit()
button start insert...and crashes @
sqlitedatabase db = data.getwritabledatabase();
i think whole problem because reason database not getting created, using plain db.execsql
statement whether in oncreate
or using db
object in data class , execsql
@ constructor crashes app.
your create statement wrong, need add integer type _id, try this:
create table record (_id integer primary key autoincrement,time text,item text,type text,amt text)
Comments
Post a Comment