Skip to content

Instantly share code, notes, and snippets.

@hansemannn
Created August 8, 2018 14:07
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 hansemannn/54f24dad214e67d19f0e5934e9f304b1 to your computer and use it in GitHub Desktop.
Save hansemannn/54f24dad214e67d19f0e5934e9f304b1 to your computer and use it in GitHub Desktop.
Use the SQLite JSON1-extension in Appcelerator Titanium.
const dbobj = require('appcelerator.encrypteddatabase');
var win = Ti.UI.createWindow({ backgroundColor: 'white' });
var btn = Ti.UI.createButton({ title: 'Trigger' });
btn.addEventListener('click', accessDatabase);
win.add(btn);
win.open();
function accessDatabase() {
dbobj.setPassword('secret');
Ti.API.info('Opening DB ...');
const instance = dbobj.open('test.db');
instance.execute('CREATE TABLE IF NOT EXISTS user(name string, phone string);');
instance.execute('insert into user (name, phone) values("oz", json(\'{"cell":"+491765", "home":"+498973"}\'));');
const dataToInsertHandle = instance.execute('select user.phone from user where user.name==\'oz\'');
const result = dataToInsertHandle.isValidRow() ? dataToInsertHandle.fieldByName('phone') : null;
alert('Fetched data: ' + result);
Ti.API.info('Closing DB ...');
instance.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment