java - Android can't add data to sqlite -
this question has answer here:
i know there lot of topics on error tried lot of solutions , still stuck whit error. it´s first time playing around sqlite, , don´t understand happening wrong there.and can't add data second activity frist activity work.
it class
public static final string key_id = "_id"; public static final string col_code_tree = "code_tree"; public static final string col_status_high = "updatestatus"; public static final string table_name_product = "product"; public static final string col_product_all = "product_all"; public static final string col_product_best = "product_best"; public static final string col_date_product = "date_product"; public databasecoconut(context context) { super(context, db_name, null, db_version); } public void oncreate(sqlitedatabase db) { string query,queryproduct; query = "create table high ( _id integer primary key, code_tree varchar, high_tree integer, date_high date, updatestatus text)"; queryproduct = "create table product ( _id integer primary key, code_tree varchar, product_all integer, product_best integer , date_product date, updatestatus text)"; db.execsql(query); db.execsql(queryproduct); } public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // db.execsql("drop table if exists "+ table_name_high); string query,queryproduct; query = "drop table if exists high"; queryproduct = "drop table if exists product"; db.execsql(query); db.execsql(queryproduct); oncreate(db); } secondactivity
public class productactivity extends appcompatactivity{ public static final string barcode_key = "barcode"; private barcode barcoderesult; private edittext result; listview listproductshow; databasecoconut mhelper; sqlitedatabase mdb2; cursor mcursor2; databasecoconut controller = new databasecoconut(this); progressdialog prgdialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_product); log.d("productactivity", "oncreate"); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); result = (edittext) findviewbyid(r.id.editcodeproduct); final edittext editall = (edittext)findviewbyid(r.id.editproductall); final edittext editbest = (edittext)findviewbyid(r.id.editproductbest); mhelper = new databasecoconut(this); mdb2 = mhelper.getwritabledatabase(); //toast.maketext(getapplicationcontext(), mhelper.getsyncstatus(), toast.length_long).show(); button buttonadd = (button)findviewbyid(r.id.buttonaddproduct); buttonadd.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { string codeproduct = result.gettext().tostring(); string = editall.gettext().tostring(); string best = editbest.gettext().tostring(); string no = "no"; if(codeproduct.trim().length() != 0 && all.trim().length() != 0 && best.trim().length() != 0 ) { cursor mcursor = mdb2.rawquery("select * " + databasecoconut.table_name_product + " " + databasecoconut.col_code_tree + "='" + codeproduct + "'" + " , " + databasecoconut.col_product_all + "='" + + "'" + " , " + databasecoconut.col_product_best + "='" + best + "'" + " , " + databasecoconut.col_status_high + "='" + no + "'" + " , " + databasecoconut.col_date_product + "='" + getdatetime() + "'", null); if(mcursor.getcount() == 0) { mdb2.execsql("insert "+ databasecoconut.table_name_product +" (" + databasecoconut.col_code_tree + ", " + databasecoconut.col_product_all + ", " + databasecoconut.col_product_best + ", " + databasecoconut.col_status_high + ", " + databasecoconut.col_date_product + ") values ('" + codeproduct + "', '" + + "', '" + best + "', '" + no + "', '" + getdatetime() + "');"); result.settext(""); editall.settext(""); editbest.settext(""); toast.maketext(getapplicationcontext() ,"เพิ่มข้อมูลเรียบร้อยแล้ว", toast.length_short).show(); } else { toast.maketext(getapplicationcontext() ,"คุณมีข้อมูลนี้อยู่แล้ว", toast.length_short).show(); } } else { toast.maketext(getapplicationcontext() ,"กรุณากรอกข้อมูลให้ครบทุกช่อง", toast.length_short).show(); } mcursor2 = mdb2.rawquery("select * " + databasecoconut.table_name_product, null); //listproductshow = (listview)findviewbyid(r.id.listhigh); //listproductshow.setadapter(updatelistview()); } }); private string getdatetime() { simpledateformat dateformat = new simpledateformat( "yyyy-mm-dd ", locale.getdefault()); date date = new date(); return dateformat.format(date); } my logcat
atal exception: main process: com.nectec.projectcoconut, pid: 27718 android.database.sqlite.sqliteexception: no such table: product (sqlite code 1): , while compiling: select * product code_tree='hi' , product_all='99' , product_best='56' , updatestatus='no' , date_product='2017-04-08 ', (os error - 2:no such file or directory) @ android.database.sqlite.sqliteconnection.nativepreparestatement(native method) @ android.database.sqlite.sqliteconnection.acquirepreparedstatement(sqliteconnection.java:897) @ android.database.sqlite.sqliteconnection.prepare(sqliteconnection.java:508) @ android.database.sqlite.sqlitesession.prepare(sqlitesession.java:588) @ android.database.sqlite.sqliteprogram.<init>(sqliteprogram.java:63) @ android.database.sqlite.sqlitequery.<init>(sqlitequery.java:37) @ android.database.sqlite.sqlitedirectcursordriver.query(sqlitedirectcursordriver.java:44) @ android.database.sqlite.sqlitedatabase.rawquerywithfactory(sqlitedatabase.java:1344) @ android.database.sqlite.sqlitedatabase.rawquery(sqlitedatabase.java:1278) @ com.nectec.projectcoconut.productactivity$1.onclick(productactivity.java:90) @ android.view.view.performclick(view.java:5264) @ android.view.view$performclick.run(view.java:21297) @ android.os.handler.handlecallback(handler.java:743) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:150) @ android.app.activitythread.main(activitythread.java:5546) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:794) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:684)
it possible executed code first time without creating "product" table in oncreate(sqlitedatabase db) method. run code same database version number in second time, not update , tells table not exist. manually uninstall application or delete data force rebuilding of database same version db_version.
Comments
Post a Comment