Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Load database, install if it does not yet exist
- Ti.API.info('Loading database');
- var db = Ti.Database.install('DB_FILE', 'DB_NAME');
- // Reinitialise database if app version is greater than database version
- db.execute('CREATE TABLE IF NOT EXISTS properties ( key TEXT , value TEXT )');
- rows = db.execute('SELECT value FROM properties WHERE key = version LIMIT 0,1');
- var database_version = (rows.rowCount) ? rows.field(0) : 0; // If there are no rows, "properties" table did not exist. Set version number to "0" to force a database update
- if (float_version(database_version) < float_version(properties.app_version)) {
- /* You can check here for specific update paths (eg. from 1.1) */
- Ti.API.info('Updating database');
- Ti.App.Properties.setString('updated_time', new Date().toString());
- // Remove old database and update with new one
- existing_database = Ti.Filesystem.getFile(Ti.Filesystem.applicationSupportDirectory+'/database/DB_NAME.sql');
- if (existing_database.exists()) { existing_database.deleteFile(); };
- db = Ti.Database.install(Ti.Filesystem.resourcesDirectory+'/data/DB_NAME.db', 'DB_NAME');
- // Get new database version variable
- database_version = db.execute('SELECT value FROM properties WHERE key = version LIMIT 0,1');
- database_version = database_version.field(0);
- };
Add Comment
Please, Sign In to add comment