java - Android SQLite table does not exist -


i trying create , insert rows in sqlite table. however, error when inserting rows table not exist. can tell me doing wrong here? thanks!

the code in activity calling class create database , insert rows follows:

    // open database     db = new dbadapter(this);     db.open();      // todo: insertrecept statements hieronder verwijderen     // dienen enkel voor test om inhoud te hebben     long id = db.insertrecept("aardappelen - bloemkool - kotelet");     id = db.insertrecept("rijst - wokgroenten - scampi - currysaus");     id = db.insertrecept("nasi goreng - omelet");     id = db.insertrecept("ebly - veggieburger - provencaalse saus");     id = db.insertrecept("rijst - kipfilet - zoetzure saus");     id = db.insertrecept("ebly - veggieburger - provencaalse saus");     id = db.insertrecept("puree - fish-sticks - spinazie");     id = db.insertrecept("tortellini - kaassaus");     id = db.insertrecept("aardappelen - bloemkool - chippolata");     id = db.insertrecept("pizza");     id = db.insertrecept("frietjes");     id = db.insertrecept("aardappel - zalm - prei - melksaus");     db.close(); 

the dbadapter class follows:

package be.bertcarremans.weekmenu;  import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.databaseutils; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log;  import java.sql.sqlexception;  public class dbadapter { static final string key_rowid = "_id"; static final string key_recept_titel = "recept_titel"; static final string tag = "dbadapter";  static final string database_name = "recepten_db"; static final string database_table = "recepten"; static final int database_version = 1;  static final string database_create =         "create table recepten (_id integer primary key autoincrement, "         + "recept_titel  text not null);";  databasehelper dbhelper; sqlitedatabase db;  final context context;  // constructor public dbadapter(context ctx) {     this.context = ctx;     dbhelper = new databasehelper(context); }  private static class databasehelper extends sqliteopenhelper {     databasehelper(context context) {         super(context,database_name,null,database_version);     }      @override     public void oncreate(sqlitedatabase db) {         db.execsql(database_create);     }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         db.execsql("drop table if exists recepten");     } }  // database openen public dbadapter open() {     db = dbhelper.getwritabledatabase();     return this; }  // database sluiten public void close() {     dbhelper.close(); }  // recept toevoegen public long insertrecept(string recept) {     contentvalues initialvalues = new contentvalues();     initialvalues.put(key_recept_titel, recept);     return db.insert(database_table, null, initialvalues); } } 

it looks have changed schema structure, , (most likely) bumped version number of database. this, in conjunction broken onupgrade() implementation caused problems.

as quick solution: uninstall app , install again - wipe database , make app create on 1st run. long term solution, need fix onupgrade(). right this:

@override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {     db.execsql("drop table if exists recepten"); } 

so in human language reads: "when need upgrade, wipe table". re-creating after that? if won't that, got no table. if got no plans have update code, i'd move table removal/creation code (respectively) i.e. createtables()/removetables() methods , make onupgrade() use it:

@override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {     removetables(db);     createtables(db); } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -