Android not paying attention to file changes? -


i'm running frustrating bug.

i have java class reads in data file, , enters database.

 package edu.uci.ics.android;  import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader;  import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper;  public class dbadapter extends sqliteopenhelper{      private static final string database_name = "mydb";     private static final int database_version = 1;     private static final string table_name = "fruits";     private static final string fruit_id = "_id";     private static final string fruit_name = "name";     private static final string file_name = "fruits.txt";     private static final string create_table = "create table "+ table_name + "("+fruit_id+" integer primary key autoincrement, "+fruit_name+" text not null);";     private sqlitedatabase mdb;     private context mcontext;      public dbadapter(context ctx){         super(ctx, database_name, null, database_version);         mcontext = ctx;         this.mdb = getwritabledatabase();     }     @override     public void oncreate(sqlitedatabase db) {         db.execsql(create_table);         // populate database         try {             bufferedreader in = new bufferedreader(new inputstreamreader(mcontext.getassets().open(file_name)));             string line;              while((line=in.readline())!=null) {                 contentvalues values = new contentvalues();                 values.put(fruit_name, line);                 db.insert(table_name, null, values);             }         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }      }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         db.execsql("drop table if exists "+table_name);         oncreate(db);        }      public cursor fetchall() {         return mdb.query(table_name, new string[] {fruit_name}, null, null, null, null, null);     }  } 

edit:

to more clear, fails:

when change database name variable, table name variable, program force closes, indicating went wrong. why can't change name of table create?

when make changes in fruits.txt file, don't see reflecting changes @ run-time. why happen?

sqliteopenhelper.oncreate() called automatically if database not exist, happen once. after that, database file exists on device's internal storage going load version has.

if want create new database when external file changed, need either delete current database file (manual process) or change current database version helper created with. when android sees version sqliteopenhelper created varies current file in internal storage, call onupgrade() allow existing database modified match new "version".

edit:

to clarify, when create database, db file created (and persisted) on device's internal storage, separate application's assets. when re-run application, persisted data storage not clear out (or else wouldn't "persisted" anymore) database file last run of application still exists...with settings when created.

when make changes variables in class, doesn't somehow magically modify database file exists on device, looking tables , databases don't exist (probably crashes coming from).

if need clear out database reflect changes you've made during development, clear database manually on device going settings -> manage applications -> {application name} -> clear data. deletes persisted files can re-created application next time launch it.

if, however, need somehow feature application automatically recognizes changes you've made file in /assets , modifies or re-creates database result, @ previous suggestions using upgrade mechanism built sqliteopenhelper

hth


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -