The example in this article describes the method for JS+HTML5 operations SQLite databases. Share to everyone for your reference, specific as follows:
//copyright by lanxyou lanxyou[at]gmail.com
Var lanxDB=function(dbname){
Var db=openDatabase(dbname,'1.0.0','',65536);
Return{
/ / Return the database name
getDBName:function(){
Return dbname;
},
/ / Initialize the database, create a table if needed
Init:function(tableName,colums){
this.switchTable(tableName);
Colums.length>0?this.createTable(colums):'';
Return this;
},
/ / Create a table, column: [name: field name, type: field type]
createTable: function(colums){
Var sql="CREATE TABLE IF NOT EXISTS " + this._table ;
Var t;
If (colums instanceof Array && colums.length>0){
t=[];
For (var i in colums){
T.push(colums[i].name+' '+colums[i].type);
}
t=t.join(', ');
}else if(typeof colums=="object"){
t+=colums.name+' '+colums.type;
}
Sql=sql+" ("+t+")";
Var that=this;
Db.transaction(function (t) {
t.executeSql(sql) ;
})
},
/ / Switch table
switchTable: function(tableName){
This._table=tableName;
Return this;
},
/ / Insert data and execute callback function, support bulk insert
//data is of type Array, each group value is of type Object, and the attribute of each Obejct should be the field name of the table, corresponding to the value to be saved.
insertData:function(data,callback){
Var that=this;
Var sql="INSERT INTO "+this._table;
If (data instanceof Array && data.length>0){
Var cols=[],qs=[];
For (var i in data[0]){
Cols.push(i);
Qs.push('?');
}
Sql+=" ("+cols.join(',')+") Values ("+qs.join(',')+")";
}else{
Return false;
}
Var p=[],
d=data,
pLenth=0,
r=[];
For (var i=0,dLength=d.length;i<dLength;i++){
Var k=[];
For (var j in d[i]){
K.push(d[i][j]);
}
P.push(k);
}
Var queue=function(b,result){
If (result){
R.push(result.insertId ||result.rowsAffected);
}
If (p.length>0){
Db.transaction(function (t) {
t.executeSql(sql,p.shift(),queue,that.onfail);
})
}else{
If (callback){
Callback.call(this,r);
}
}
}
Queue();
},
_where:'',
//where statement, support self-write and form of object attribute value pairs
Where:function(where){
If (typeof where==='object'){
Var j=this.toArray(where);
This._where=j.join(' and ');
}else if (typeof where==='string'){
This._where=where;
}
Return this;
},
/ / Update the data, data is the attribute value pair form
updateData:function(data,callback){
Var that=this;
Var sql="Update "+this._table;
Data=this.toArray(data).join(',');
Sql+=" Set "+data+" where "+this._where;
this.doQuery(sql,callback);
},
/ / Save data according to the conditions, if it exists, update, if there is no, insert data
saveData:function(data,callback){
Var sql="Select * from "+this._table+" where "+this._where;
Var that=this;
this.doQuery(sql,function(r){
If (r.length>0){
that.updateData(data,callback);
}else{
that.insertData([data],callback);
}
});
},
//retrieve data
getData:function(callback){
Var that=this;
Var sql="Select * from "+that._table;
That._where.length>0?sql+="where "+that._where:"";
that.doQuery(sql,callback);
},
/ / Query, internal methods
doQuery: function(sql,callback){
Var that=this;
Var a=[];
Var bb=function(b,result){
If (result.rows.length){
For (var i=0;i<result.rows.length;i++){
A.push(result.rows.item(i));
}
}else{
A.push(result.rowsAffected);
}
If (callback){
Callback.call(that,a);
}
}
Db.transaction(function (t) {
t.executeSql(sql,[],bb,that.onfail) ;
})
},
/ / Delete data according to conditions
deleteData:function(callback){
Var that=this;
Var sql="delete from "+that._table;
That._where.length>0?sql+="where "+that._where:'';
that.doQuery(sql,callback);
},
/ / Delete the table
dropTable:function(){
Var sql="DROP TABLE IF EXISTS "+this._table;
this.doQuery(sql);
},
_error:'',
Onfail:function(t,e){
This._error=e.message;
Console.log('----sqlite:'+e.message);
},
toArray:function(obj){
Var t=[];
Obj=obj || {};
If (obj){
For (var i in obj){
T.push(i+"='"+obj[i]+"'");
}
}
Return t;
}
}
}
/*
Examples:
Var db=new lanxDB('testDB');
Db.init('channel_list',[{name:'id',type:'integer primary key autoincrement'},{name:'name',type:'text'},{name:'link',type:' Text'},{name:'cover',type:'text'},{name:'updatetime',type:'integer'},{name:'orders',type:'integer'}]);
Db.init('feed_list',[{name:'parentid',type:'integer'},{name:'feed',type:'text'}]);
db.switchTable('channel_list').insertData([{name:'aa',link:'ss',updatetime:new Date().getTime()},{name:'bb',link:'kk', Updatetime:new Date().getTime()}]);
Db.where({name:'aa'}).getData(function(result){
Console.log(result);//result is Array
});
Db.where({name:'aa'}).deleteData(function(result){
Console.log(result[0]);//delete number
});
Db.where({name:'bb'}).saveData({link:'jj'},function(result){
Console.log(result);//Impact number
})
})
*/