Skip to content

Instantly share code, notes, and snippets.

@egomez99
Created September 3, 2013 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egomez99/6418772 to your computer and use it in GitHub Desktop.
Save egomez99/6418772 to your computer and use it in GitHub Desktop.
Database PRAGMA commmand
var object = require('database').win();
object.open();
exports.win = function() {
var self = Titanium.UI.createWindow({
height : 30,
width : 280,
borderRadius : 10,
bottom : 140,
backgroundColor : '#333'
});
var db = Titanium.Database.install('etc/testdb.db', 'quotes');
var pragma = db.execute('PRAGMA table_info(tips)');
while (pragma.isValidRow()) {
Titanium.API.info( pragma.field(0) + ' - ' + pragma.fieldName(0) );
Titanium.API.info( pragma.field(1) + ' - ' + pragma.fieldName(1) );
pragma.next();
}
var uv = db.execute('pragma user_version');
Ti.API.info( ' pragma user_version = '+ JS(uv));
var rows = db.execute('SELECT * FROM TIPS');
db.execute('UPDATE TIPS SET TITLE="UPDATED TITLE" WHERE TITLE = "FOO"');
//db.execute('INSERT INTO TIPS VALUES("FOO", "BAR")');
//db.execute("COMMIT");
while (rows.isValidRow()) {
Titanium.API.info(rows.field(1) + '\n' + rows.field(0) + ' col 1 ' + rows.fieldName(0) + ' col 2 ' + rows.fieldName(1));
rows.next();
}
// close database
rows.close();
self.addEventListener('click', function(e) {
self.close();
});
self.addEventListener('open', function() {
setTimeout(function() {
self.close();
}, 3000);
});
return self;
};
function JS (rows){
var data = null, i = 0;
while (rows.isValidRow()) {
Ti.API.info(' i: '+ i);
data += rows.field(i); + ' \n ' + ' Column: ' + rows.fieldName(i) + '\n\n\n';
//data += rows.field(1) + ' \n ' + ' Column: ' + rows.fieldName(1) + '\n\n\n';;
//data += rows.field(2) + ' \n ' + ' Column: ' + rows.fieldName(2) + '\n\n\n';;
rows.next(); i++;
}
return JSON.stringify(data);
}
/*
(1) Run snippet
[INFO] 0 - cid
[INFO] title - name
[INFO] 1 - cid
[INFO] tip - name
[INFO] 2 - cid
[INFO] newfield - name
[INFO] i: 0
[INFO] pragma user_version = 0
(2) Alter your DB (MesaSqlite can be used for this purposes)
(3) Upgrade application version field (you can just change from v1.0 to v1.1 at tiapp.xml)
(4) Run snippet again and check console output:
[INFO] 0 - cid
[INFO] title - name
[INFO] 1 - cid
[INFO] tip - name
[INFO] 2 - cid
[INFO] tipCopyl - name
[INFO] i: 0
[INFO] pragma user_version = 1
Pragma version detects schema changes
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment