Delete/Drop Database
I have an app with an local database. It checks an remote server and downloads an newer database file if the remote one is newer.
But now i need to use the new one. So i would like to drop the already installed one or force using the new one when using the Database.install call. How can i do this?
5 Answers
-
So you just need to know how to drop the DB, or you need to check to see if the downloaded db is newer?
sqlite drop example:
db.execute("DROP TABLE IF EXISTS mydb"); -
Yeah but i would like to drop the complete database. I already checked the source code, and this function is the in the core but it seems you cant access it through the API..
I guess this should be on the wish list for a future release..
-
Jan,
Did you find out how to drop the DB? I'd like to do the same thing. I tried the sqlite example from Rob, but that gives me a syntax error in Titanium mobile. -
How's this? (Android)
var f = Titanium.Filesystem.getFile('file:///data/data/' + Ti.App.getID() + '/databases', db_name);
f.deleteFile(); -
You should put a table in the database with a versionnumber, and compare the versionnumber of the database with the versionnumber of the remote server.
Not tested, but I think it should be something like this:
var db = Ti.Database.open('database'); var dbCheck = db.execute('SELECT id, version FROM dbcheck WHERE id=?',1); while (dbCheck.isValidRow()){ var dbId = dbCheck.fieldByName('id'); var dbControle = dbCheck.fieldByName('version'); if (dbControle < VERSIONNUMER_OF_REMOTE_SERVER){ var outofdate = true; } else { var outofdate = false; }; dbCheck.next(); }; dbCheck.close(); if(outofdate == true){ //delete old database code here //install new database code here };
Let us know if it worked