Web SQL Practice under PhoneGap

Source: Internet
Author: User
Tags sqlite database

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.