android - Refreshing SQL database -


i'm using databasehelper class load sqlite database. if doesn't exist, create it. xml file used create database in specific folder on sdcard.

i reload sql database xml file via menu. have menu setup, can't seem find way reload sql. there easy way without having uninstall app , reinstall removes/recreates database.

here's code databasehelper:

package samples.myapp;  import java.io.file; import java.io.fileinputstream; import java.io.inputstream; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import org.w3c.dom.document; import org.w3c.dom.nodelist; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.widget.toast;  public class databasehelper extends sqliteopenhelper {      public static final string database_name = "projectdocuments";      protected context context;      public databasehelper(context context) {         super(context, database_name, null, 1);         this.context = context;     }      @override     public void oncreate(sqlitedatabase db) {         string s;         try {             toast.maketext(context, "database created", 2000).show();             // old database loaded res             //inputstream in = context.getresources().openrawresource(r.raw.sql);             // load sdcard             inputstream in = new fileinputstream("/sdcard/contracthound/sql.xml");              documentbuilder builder = documentbuilderfactory.newinstance().newdocumentbuilder();             document doc = builder.parse(in, null);             nodelist statements = doc.getelementsbytagname("statement");             (int i=0; i<statements.getlength(); i++) {                 s = statements.item(i).getchildnodes().item(0).getnodevalue();                 db.execsql(s);             }         } catch (throwable t) {             toast.maketext(context, t.tostring(), 90000).show();         }     }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         db.execsql("drop table if exists documents");         oncreate(db);     }    } 

do mean want invoke onupgrade? if so, increase db version in constructor

super(context, database_name, null, 2); 

or programmatically

getwritabledatabase().setversion(2); 

edited

databasehelper dbhelper = new databasehelper(); sqlitedatabase db = dbhelper.getwritabledatabase(); db.setversion(db.getversion()+1); 

something that. i've never incremented db version other hardcoding it.
, don't forget close db in onstop or somewhere else calling db.close() or dbhelper.close().
there's method needupgrade (int newversion) has no javadoc, try play it.


Comments

Popular posts from this blog

Cursor error with postgresql, pgpool and php -

delphi - ESC/P programming! -

c++ - error: use of deleted function -