The common operation of the Websql is a package, the project is based on PhoneGap, but the PHONEGAP default has integrated the database operation plug-in, so no need to configure what, directly use can be:
/*** Database Operation helper class, define object, data operation method are defined here */var dbname= ' mydb '; var version= ' 1.1 '; var dbdesc= ' mydb '; var table_picture= ' Be_ Picture '; var dbsize=30000;var db=null;/** * Open Database * @returns {Boolean} */function opendb (callback) {try{if (!window.opend Atabase) {console.log (' The browser does not support database '); return false; } db = Window.opendatabase (dbname, version, Dbdesc, dbsize); return true;} catch (E) {if (e==2) {console.log ("Invalid database Version");} Else{console.log ("Unknown error" +e+ "."); return false;}} /** * Executes a SQL * @param sql */function execsql (sql,param,callback) {if (db==null) {opendb ();} Db.transaction (Function (TX) {tx.executesql (Sql,param, function (TX, result) {if (typeof (callback) = = ' function ') { Callback (TRUE)}return true;}, function (TX, error) {if (typeof (callback) = = ' function ') {callback (false)}console.log ( Error); return false;});}); var picturefields=[' id ', ' app_flow_no ', ' ptr_type ', ' ptr_name ', ' ptr_address ', ' Blurred ', ' original ', ' Lo Cal_address ']/** * Initialize database */function Initdb () {if (db==null) {opendb ();} CreateTable (table_picture,picturefields,{"id": "PRIMARY Key", "App_flow_no": "Not NULL"}); /** * CREATE DATABASE * @param tablename table name * @param fields field * @param constraint constraints or other additions to a field, can be blank, * format: {"id": "Integer primary key AutoIncrement "," app_flow_no ":" Not Null "} */function createtable (tablename,fields,constraint) {if (db==null) {OpenDB ( );} var sql = ' CREATE TABLE IF ' EXISTS ' +tablename+ ' ('; for (i-Fields) {var key = ""; if (typeof (constraint)! = "undefined" &A mp;& typeof (Constraint[fields[i])! = "undefined") {key = "" +constraint[fields[i]];} Sql+=fields[i]+key+ ",";} sql = SQL.SUBSTR (0,sql.length-1); SQL + = ")";//log (sql); execsql (SQL);} /** * Update data * @param tablename table name * @param setfields array of fields to update * @param setparams the parameter array for the field to update * @param wherestrwhere statement, if there is no Does not pass, does not contain the WHERE keyword, the parameter uses instead, such as: Id=? and name=? * @param the parameter array used by the Wherparamswhere statement, such as [' 111 ', ' 2222 '] */function updateTable (TABLENAME,SETFIELDS,SETPARAMS,WHERESTR, wherparams) {var sql = "Update" +tablename+ "set"; for (i in Setfields) {sql+=setfields[i]+ "=?,";} sql = SQL.SUBSTR (0,sql.length-1); if (typeof (WHERESTR)! = "undefined" && typeof (Wherparams)! = "Undefined" & & Wherestr!= "") {SQL + = "where" + Wherestr;setparams = Setparams.concat (wherparams);} Execsql (sql,setparams);} /** * Insert Data * @param tableName * @param insertfields * @param insertparams */function inserttable (tablename,insertfields,in sertparams) {var sql = ' insert INTO ' +tablename+ ' ("; var sql2 =" values ("; for (i in Insertfields) {sql+=insertfields[i]+", "; Sql2 + = "?,"}sql = Sql.substr (0,sql.length-1); sql2 = Sql2.substr (0,sql2.length-1); SQL + = ")"; Sql2 + = ")"; Execsql (sql+ Sql2,insertparams);} /** * Delete Data * @param tableName * @param wherestr * @param wherparams */function deleterow (tablename,wherestr,wherparams) {VA R sql = "Delete from" +tablename;if (typeof (WHERESTR)! = "undefined" && typeof (Wherparams)! = "Undefined" & & Wherestr!= "") {SQL + = "where" + Wherestr;} Execsql (sql,wherparams);} /** * Query * @param tableName * @param selectfieldsselect fields, separated by commas, if all pass "*" * @param wherestrwhere statements, parameters used instead of * @param wherparams parameter array * @callback required, the returned object will be placed in the callback function as a parameter passed */function select (tablen Ame,selectfields,wherestr,wherparams,callback) {if (db==null) {opendb ();} var sql = "Select" +selectfields+ "from" +tablename;if (typeof (WHERESTR)! = "undefined" && typeof (Wherparams)! = " Undefined "&& wherestr!=" ") {SQL + =" where "+ Wherestr;} Db.transaction (Function (TX) {Tx.executesql (Sql,wherparams,function (tx,results) {if (RESULTS.ROWS.LENGTH&L t;1) {if (typeof (callback) = = ' function ') {callback (FALSE)}//No data}else{if (typeof (Callb ACK) = = ' function ') {callback (Results.rows)}}},function (Tx,error) {return false; }); });} /** * INSERT or UPDATE * @param tableName * @param insertfields * @param insertparams * @param key according to the key to determine if there is data * @param keyval */FU Nction saveorupdate (tablename,insertfields,insertparams,key,keyval) {if (typeof (key)! = "undefined" && typeof (keyval)! = "Undefined "&& key!=" ") {select (tablename,insertfields[0],key+" =? ", [Keyval],function (rows) {if (rows) { UpdateTable (tablename,insertfields,insertparams,key+ "=?", [Keyval]);} Else{insertfields.push (key); Insertparams.push (keyval); inserttable (Tablename,insertfields,insertparams);}})} Else{inserttable (Tablename,insertfields,insertparams);}}
Inquire:
Select (Table_picture, "*", "id=?", [Id],function (rows) {if (rows) {//If query to Data}})
Note: Because Websql is executed asynchronously, to get to the returned result and then proceed further, you need to pass in the callback function, such as the function above (rows) ....
Insert or UPDATE:
Saveorupdate (table_picture,[' id ', ' app_flow_no ', ' original ', ' Ptr_type ', ' Ptr_name '],[pic.id,pic.appflowno, Pic.original,pic.ptrtype,pic.ptrname], ' id ', pic.id);
Delete:
DeleteRow (Table_picture, "id=?", [123])
Transferred from: http://blog.csdn.net/linshutao/article/details/21398483
Websql using/PHONEGAP to manipulate the database SQLite