With this library you'll be able to use SQLite databases in your Titanium apps using a JavaScript interface. I personally feel comfortable with SQL Syntax, but prefer to use this library because makes the code more readable.
This is not a replacement for Alloy Models.
If you're looking for a full-blown ORM for Titanium you might want to check out Joli.js.
// require the module
var DBH=require('com.alcoapps.dbhelper');
// create an instance with your local db
var db=new DBH.dbhelper('/alco.sqlite','alco');
Returns a JSON object with the full result set.
Returns JSON.
var myTable=db.get({
fields : '*',
table : 'events',
where : 'country like "U%"',
order : 'id DESC'
});
Send a callback function .
db.get({
fields : '*',
table : 'events',
where : 'country="US"',
order : 'id DESC'
},function(evt){
console.log(evt);
});
Returns JSON.
db.get({
joiner : 'e'
fields : 'e.*',
table : 'events e, parties p',
where : 'e.country="US" AND p.event_id=e.id',
order : 'eid DESC'
},function(evt){
console.log(evt);
});
Returns the Id of the last inserted row.
db.getLastId({
table: 'events'
},function(evt){
console.log(evt);
});
Returns the last inserted row object.
db.getLastEntry({
table: 'events'
},function(evt){
console.log(evt);
});
Returns the Id of the last inserted row.
var rowId=db.set({
table: 'events',
data : {
country : 'Puerto Rico',
name : 'TiConf PR'
}
});
Returns the amount of rows affected by the edit.
var rowsAffected=db.update({
table : 'events',
data :{
name : 'xTiConf NY',
country : 'PR'
},
where : 'id = 1'
});
or
var rowsAffected=db.update({
table : 'events',
data :{
name : 'xTiConf NY',
country : 'PR'
},
id: 1
});
Returns the amount of rows affected by the delete.
var rowsAffected=db.delete({
table : "events",
where : 'name="xTiConf PR"'
});
Takes an SQL String and returns a JSON object with the result set
var myTable=db.exec('SELECT * FROM events where id > 5');
Returns an image from a Blob column. In this example, assuming you have a column named 'images' inside a table named 'myimages', grab the image with ID=1
var img=db.getImage({
field: 'image',
table: 'myimages',
where: 'id=1'
});
win.backgroundImage=img;
or
db.getImage({
field: 'image',
table: 'myimages',
where: 'id=1'
},function(img){
win.backgroundImage=img;
});
Used when you only want to get a single value from a database record
var userEmail=db.getEntry({
table: 'users',
id: '5'
field: 'email'
});
or
db.getEntry({
table: 'users',
id: '5'
field: 'email'
},function(value){
console.log(value);
});
Returns the amount of rows returned by a give query
var totalActiveUsers=db.countRows({
table: 'users',
where: 'active=true'
});
or
db.countRows({
table: 'users',
where: 'active=true'
},function(value){
console.log(value);
});
or with join
db.countRows({
joiner: 'u',
table: 'users u, images i',
where: 'u.active=true AND u.id = i.image_id'
},function(value){
console.log(value);
});
Takes a flat JSON string and creates a table. Good for times when you get data from a Web Service and then need to search or manipulate the data in any way.
db.createFromJSON(jsonString,'mytable');
Deletes a table
db.drop('myTable');
Alter a table if the field doesn't exist
db.addColumn('user', 'street', 'VARCHAR');
Checks if a given table exists.
var exists=db.tableExists('myTable');
Closes the database connection
db.close();
- Ricardo Alcocer - Creator and maintainer
- Sebastian Klaus
- Rick Blalock