android - Error Code : 1 (SQLITE_ERROR) Caused By : SQL(query) error or missing database -
by click on snackbar want add page favourite list face error when click snackbar:
error code : 1 (sqlite_error) caused : sql(query) error or missing database. (no such table: ghavaed (code 1): , while compiling: insert ghavaed(saal,title) values (?,?))
this first project please help
here page class want add favourite lists:
public class styleghaede extends appcompatactivity { string title, tozih, sarfasl, saal; textview style_ghaede_content; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); bundle extras = getintent().getextras(); title = extras.getstring("title"); tozih = extras.getstring("tozih"); sarfasl = extras.getstring("sarfasl"); saal = extras.getstring("saal"); setcontentview(r.layout.activity_style_ghaede); getcomponent(); style_ghaede_content.settext(tozih); } private void getcomponent(){ toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); getsupportactionbar().settitle(title); final database db=new database(this); final ghavaed fq=new ghavaed(); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { snackbar.make(view, "", snackbar.length_long) .setaction("برای اضافه کردن به لیست علاقه مندی کلیک کنید", new view.onclicklistener() { @override public void onclick(view v) { db.addtofavourite(fq.getsaal(),fq.gettitle()); } }).show(); } }); style_ghaede_content = (textview) findviewbyid(r.id.style_ghaede_content); } }
my favouritelist class:
public class favourite_list extends appcompatactivity { list<ghavaed> favouritelist; database db=new database(this); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_favourite__list); favouritelist=db.getfavouriteqavaed(); listview listview = (listview) findviewbyid(r.id.list_view_fav); final adapter_favourite adapter_fav = new adapter_favourite(getapplicationcontext(),favouritelist); listview.setadapter(adapter_fav); ghavaed fq = new ghavaed(); //swipelist swipelayout swipelayout = (swipelayout)findviewbyid(r.id.fav_item); swipelayout.setshowmode(swipelayout.showmode.laydown); swipelayout.adddrag(swipelayout.dragedge.left, findviewbyid(r.id.bottom_wrapper)); swipelayout.addswipelistener(new swipelayout.swipelistener() { @override public void onclose(swipelayout layout) { //when surfaceview totally cover bottomview. } @override public void onupdate(swipelayout layout, int leftoffset, int topoffset) { //you swiping. } @override public void onstartopen(swipelayout layout) { } @override public void onopen(swipelayout layout) { //when bottomview totally show. } @override public void onstartclose(swipelayout layout) { } @override public void onhandrelease(swipelayout layout, float xvel, float yvel) { //when user's hand released. } }); } }
my database:
public class database extends sqliteopenhelper { private static final int database_version = 1; // database name private static final string database_name = "database"; private static final string table_favourite_ghavaed = "ghavaed"; private static final string tozih = "tozih"; private static final string saal = "saal"; private static final string title = "title"; private static final string tag = "database"; public database(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { string create_favourite_ghavaed_table = "create table " + table_favourite_ghavaed + "(" + saal + " text," + title + " text" + ")"; db.execsql(create_favourite_ghavaed_table); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + table_favourite_ghavaed); // create tables again oncreate(db); } public void addtofavourite(string saal, string title) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(saal, saal); // product name values.put(title, title); // product number // inserting row db.insert(table_favourite_ghavaed, null, values); db.close(); // closing database connection // log.i("setuserexam", "addtofavourite : done!!"); } public void removefavourite(string saal, string title) { sqlitedatabase db = this.getwritabledatabase(); db.execsql("delete " + table_favourite_ghavaed + " saal ='" + saal + "' , title = '" + title + "' "); db.close(); } public list<ghavaed> getfavouriteqavaed() { list<ghavaed> favouriteqavaedlist = new arraylist<>(); sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery("select * ghavaed", null); if (cursor.movetofirst()) { { ghavaed favouriteqavaed = new ghavaed(); favouriteqavaed.setsaal(cursor.getstring(0)); favouriteqavaed.settitle(cursor.getstring(3)); // favouriteqavaed.settozih(cursor.getstring(2)); // adding contact list favouriteqavaedlist.add(favouriteqavaed); } while (cursor.movetonext()); } db.close(); return favouriteqavaedlist; } public integer getfavouritesnumber() { sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery("select * ghavaed", null); return cursor.getcount(); } public boolean checkisfavourite(string saal, string title) { sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery("select * ghavaed saal = '" + saal + "'" + "and title = '" + title + "' ", null); if (cursor != null && cursor.getcount() > 0) { cursor.movetofirst(); db.close(); return true; } else { db.close(); return false; } } } public class ghavaed { string saal, sarfasl, tozih, title; public string getsaal() { return saal; } public void setsaal(string saal) { this.saal = saal; } public string getsarfasl() { return sarfasl; } public void setsarfasl(string sarfasl) { this.sarfasl = sarfasl; } public string gettozih() { return tozih; } public void settozih(string tozih) { this.tozih = tozih; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } }
Comments
Post a Comment