Skip to content

Instantly share code, notes, and snippets.

@egomez99

egomez99/app.js

Created Sep 3, 2013
Embed
What would you like to do?
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
You can’t perform that action at this time.