Wrapper(Middleware) for websql DB transactions. Works well with Cordova sqlite plugin 'https://github.com/brodysoft/Cordova-SQLitePlugin'
Installation using bower:
bower install websql-helper
Include this script file in your html.
<script src="dbhandler.js"></script>Create Database:
DBHandler.initDatabase();
Create a table:
DBHandler.isExists("user", function(result){
if( parseInt(result.rows.item(0).count) == 0 ){
DBHandler.createTable("user","sid INTEGER primary key, name TEXT, created_date TIMESTAMP", function (result) {
});
}
});
Insert New Record:
var currentDate = Date.today().getTime();
var columns = ["sid", "name", "created_date"];
var values = [1 , "John", currentDate];
DBHandler.newRecord("user", columns, values, function (result) {
console.log(result.insertId+"rows affected"+result.rowsAffected);
if(result.rowsAffected){
console.log("inserted");
}
});
Update Record:
var columns = ["name", "created_date"];
var values = ["John", currentDate];
DBHandler.updateRecord("user", "sid = 1", columns, values, function(result){
console.log("course updated");
if(result.rowsAffected){
}
});
Get Record:
DBHandler.getRecord("user", "*", "sid = "+Student.studentID, function (result) {
if(result.rows.length){
// Get the first record details
var createdDate = result.rows.item(0).created_date,
name = result.rows.item(0).name,
studentID = result.rows.item(0).sid;
// Loop through to get all the records
for(var i=0; i < result.rows.length; i++){
student = result.rows.item(i);
console.log(student);
};
}
});
Get All Records:
DBHandler.getAllRecords("user", function (result) {
if(result.rows.length){
// Get the first record details
var createdDate = result.rows.item(0).created_date,
name = result.rows.item(0).name,
studentID = result.rows.item(0).sid;
// Loop through to get all the records
for(var i=0; i < result.rows.length; i++){
student = result.rows.item(i);
console.log(student);
};
}
});
Delete Record:
DBHandler.deleteRecord("user", "sid = 1", function(result){
console.log("deleted");
});
Drop Table:
DBHandler.dropTable("user", function(result){
console.log(result);
});