Click here to download the jQuery database plugin.
$.db.open_db$.db.create_table (see example below)true) or override if already exists (false)$.db.query$.db.get()This function is used to retrieve data from the table specified in the first argument.
$.db.get('prayers')
$.db.where()This function specifies the where clause.
$.db.where('id', 1).get('prayers')
$.db.order_by()This function specifies how to order the returning data. The first argument is the field, the second is the direction (DESC: descending, or ASC: ascending).
$.db.order_by('date', 'DESC').get('prayers')
$.db.limit()This function specifies a limit on how many rows to load. The first argument is the limit, the second is the offset.
$.db.limit(10, 20).get('prayers');
$.db.insert()This function inserts data from the second argument into a table specified in the first argument. The second argument is an object.
$.db.insert('prayers', {id: null, prayer: 'For my fiancée\'s upcoming test.', date: (new Date()).getTime()})
$.db.update()This function updates the data from the second argument in a table specified in the first argument. The second argument is an object. If a where clause is specified, it limits what is updated.
$.db.where('id', 1).update('prayers', {date: (new Date()).getTime()})
$.db.del()This function deletes data from the table specified in the first argument. If a where clause is specified, it limits what is updated.
$.db.where('id', 1).del('prayers')
//connect to database
$.db.open_db({
shortName: 'prayers',
version: '1.0',
name: 'Prayers',
maxSize: 1048576 //allow up to 1 mb to be stored
});
//create a table, named "prayers"
$.db.create_table({
name: 'prayers',
fields: [{
name: 'id',
type: 'INTEGER',
extra:'PRIMARY KEY AUTOINCREMENT'
}, {
name: 'prayer'
}, {
name: 'date',
type: 'INTEGER'
}]
}, true);
//select last 5 prayers from "prayers" table & append them to the body
$.db.order_by('date', 'DESC').limit(5).get('prayers', function (r) {
for (var i=0, len=r.num_rows; i<len; i++) {
$('body').append('<div>'+r.results[i].prayer+'</div>');
}
}, function () {
alert('an error has occurred');
});