Android Sqlite "No Such Table" Error -


this question has answer here:

i trying learn sqlite databases in android can not handle simple jobs. when insert record in database "insertword" got error. seems interesting because types table created , inserted values program. doing wrong?.

this code (database.java).

package com.pekgenc.mehmet.spinnerexample;  import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteexception; import android.database.sqlite.sqliteopenhelper; import android.support.annotation.nonnull; import android.util.log;  import java.util.arraylist; import java.util.collection; import java.util.hashmap; import java.util.iterator; import java.util.list; import java.util.listiterator;  /**  * created mehmet on 18.08.2017.  */  public class database extends sqliteopenhelper {      // database version     private static final int database_version = 1;      // database name     private static final string database_name = "mywordbank";      // labels table name     private static final string table_types = "types";      // labels table column names     private static final string key_id = "id";     private static final string key_name = "name";      // words table name     private static final string table_words = "words";      // words table column names     private static final string wkey_id = "id";     private static final string wkey_tur_name = "tur_id";     private static final string wkey_english = "english";     private static final string wkey_turkish = "turkish";     private static final string wkey_example = "example";      public database(context context) {         super(context, database_name, null, database_version);     }      @override     public void oncreate(sqlitedatabase db) {         string create_categories_table = "create table " + table_types + "("                 + key_id + " integer primary key autoincrement," + key_name + " text)";          string create_words_table = "create table " + table_words + "("                 + wkey_id + " integer primary key autoincrement," + wkey_tur_name + " integer,"                 + wkey_english + " text," + wkey_turkish + " text," + wkey_example + " text)";          db.execsql(create_categories_table);         try {             db.execsql(create_words_table);         }         catch (sqliteexception es)         {             log.e("err: ", es.getmessage());         }           // inserting label values         db.execsql("insert " + table_types + "(" + key_name + ")" + " values ('adjective')");         db.execsql("insert " + table_types + "(" + key_name + ")" + " values ('adverb')");         db.execsql("insert " + table_types + "(" + key_name + ")" + " values ('conjunction')");         db.execsql("insert " + table_types + "(" + key_name + ")" + " values ('determiner')");         db.execsql("insert " + table_types + "(" + key_name + ")" + " values ('idiom')");         db.execsql("insert " + table_types + "(" + key_name + ")" + " values ('interjection')");         db.execsql("insert " + table_types + "(" + key_name + ")" + " values ('noun')");         db.execsql("insert " + table_types + "(" + key_name + ")" + " values ('phrasal verb')");         db.execsql("insert " + table_types + "(" + key_name + ")" + " values ('phrase')");         db.execsql("insert " + table_types + "(" + key_name + ")" + " values ('preposition')");         db.execsql("insert " + table_types + "(" + key_name + ")" + " values ('pronoun')");         db.execsql("insert " + table_types + "(" + key_name + ")" + " values ('verb')");         db.execsql("insert " + table_types + "(" + key_name + ")" + " values ('other')");     }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         db.execsql("drop table if exists " + table_types);         db.execsql("drop table if exists " + table_words);         oncreate(db);     }      // types     public list<string> getalllabels() {         list<string> labels = new arraylist<string>();          string selectquery = "select * " + table_types;         sqlitedatabase db = this.getreadabledatabase();         cursor cursor = db.rawquery(selectquery, null);          if (cursor.movetofirst()) {             {                 labels.add(cursor.getstring(1));             } while(cursor.movetonext());         }          cursor.close();         db.close();          return labels;     }      // inserting word in database     public void insertword(int tur_id, string english, string turkish, string example) {         sqlitedatabase db = this.getwritabledatabase();          contentvalues values = new contentvalues();         values.put(wkey_tur_name, tur_id);         values.put(wkey_english, english);         values.put(wkey_turkish, turkish);         values.put(wkey_example, example);          try {             db.insertorthrow(table_words, null, values);         }         catch (sqliteexception e) {             log.e("err: ", e.getmessage());         }         db.close();     }      public list<string> fetchallwords() {         list<string> words = new arraylist<string>();         string selectquery = "select english, turkish " + table_words;          sqlitedatabase db = this.getreadabledatabase();         cursor cursor = db.rawquery(selectquery, null);          if (cursor.movetofirst()) {             {                 words.add(cursor.getstring(1));             } while(cursor.movetonext());         }          cursor.close();         db.close();          return words;     } } 

(addactivity.java)

package com.pekgenc.mehmet.spinnerexample;  import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.spinner; import android.widget.toast;  import java.util.list;  public class addactivity extends appcompatactivity {      edittext texteng, texttur, textexp;     button btnadd, btnback;     spinner sp;     arrayadapter<string> adaptor;     int selectedindex;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_add);          texteng = (edittext) findviewbyid(r.id.edittextenglish);         texttur = (edittext) findviewbyid(r.id.edittextturkish);         textexp = (edittext) findviewbyid(r.id.edittextexample);         btnadd  = (button) findviewbyid(r.id.buttonadd);         btnback = (button) findviewbyid(r.id.buttonback);         sp      = (spinner)findviewbyid(r.id.spinnertype);          loadspinnerdata();          sp.setonitemselectedlistener(new adapterview.onitemselectedlistener() {             @override             public void onitemselected(adapterview<?> parent, view view, int position, long id) {                 selectedindex = parent.getselecteditemposition() + 1;             }              @override             public void onnothingselected(adapterview<?> parent) {              }         });          btnadd.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 database db = new database(getapplicationcontext());                 db.insertword(selectedindex, texteng.gettext().tostring(), texttur.gettext().tostring(), textexp.gettext().tostring());                 db.close();             }         });     }      private void loadspinnerdata() {          database db = new database(getapplicationcontext());         list<string> labels = db.getalllabels();          adaptor = new arrayadapter<string>(this, android.r.layout.simple_spinner_dropdown_item, labels);         adaptor.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);         sp.setadapter(adaptor);     } } 

(logcat)

08-20 20:29:42.621 16118-16118/com.pekgenc.mehmet.spinnerexample e/sqlitelog: (1) no such table: words 08-20 20:29:42.622 16118-16118/com.pekgenc.mehmet.spinnerexample e/err:: no such table: words (code 1): , while compiling: insert words(example,english,turkish,tur_id) values (?,?,?,?) 

thank you.

even though lot of code better no code, isn't place people willing find bugs you're lazy for. code seems ok, had db when added words table? try removing app , launch again, should recreate tables safely. if that's issue, use db version field in future

i recommend using greendao sqlite android, makes things lot easier


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -