HTML5 Web SQL database, built-in SQLite database,
Operations on the database use ExecuteSQL to perform additions and deletions
1. Create a database
function Creatdatabase () { db = OpenDatabase (' Student ', ' 1.0 ', ' stumanage ', 2 * 1024 * 1024);}
2. Create a table
function CreateTable () {if (db) { var strSQL = "CREATE table if not exists stuinfo"; strSQL + = "(Stuid unique,name text,sex text,score int)"; Db.transaction (Function (TX) { tx.executesql (strSQL) }, function () { console.log ("CREATE table Error"); }, function () { console.log ("CREATE table succeeded"); }) }
3. Add Data
function AddData () { if (db) { var strSQL = "INSERT into stuinfo values"; strSQL + = "(?,?,?,?)"; Db.transaction (Function (TX) { tx.executesql (strsql,[ $$ ("Txtstuid"). value,$$ ("Txtname"). Value, $$ (" Selsex "). value,$$ (" Txtscore "). Value],function () {$$ (" txtname "). value=" "; $$ ("Txtscore"). value= ""; Alert ("added 1 records successfully!");},function (Tx,ex) { console.log (ex.message)})} }
4. Querying data
function Querydata () { if (db) { var sql = "Select * from Stuinfo"; Db.transaction (Function (TX) { Tx.executesql (sql,[],function (TX, results) {var len = Results.rows.length,i;var HTMLData = ""; for (i = 0; i < Len; i++) { var item = Results.rows.item (i); var Htmlitem = Item. Stuid + "" + Item. Name + "" + Item. Sex + "" + Item. Score; HTMLData + = Htmlitem + "</br>";} $$ ("Info"). InnerHTML = Htmldata;},function (tx,ex) { console.log (ex.message)})} )} }
5. Delete data
function DeleteData () { if (db) { var strSQL = "Delete from stuinfo where stuid =?"; Db.transaction (Function (TX) { tx.executesql (strsql,[ $$ ("Txtstuid"). Value],function () { alert (" Successfully deleted 1 records! ");},function (Tx,ex) { console.log (ex.message)})} ) } Querydata ();}
The complete code is as follows
<! DOCTYPE html>
Web SQL Practice under PhoneGap