First, Introduction
This API is based on the web SQL Database specification and Web Storage API specification.
Some devices already provide implementations of this specification, with built-in implementations instead of using PHONEGAP implementations for these devices.
For devices that do not have storage support, the PHONEGAP implementation should be fully compatible with the specifications.
Second, OpenDatabase method
Window.opendatabase (name, version, display_name, size)
The method will create a new SQL Lite database and return the Databases object. You can manipulate data using the database object.
Name: The names of the databases.
Version: The revision number of the database.
Display_name: The display name of the database.
Size: The database sizes in bytes.
<!DOCTYPE HTML> <HTML><Head><MetaCharSet= "Utf-8"><title>JQuery Mobile WEB Application</title><Linkhref=".. /jquery.mobile-1.3.2.css "rel= "stylesheet"type= "Text/css"/><Scriptsrc=".. /jquery.js "type= "Text/javascript"></Script><Scriptsrc=".. /jquery.mobile-1.3.2.js "type= "Text/javascript"></Script><Scriptsrc=".. /cordova.js "type= "Text/javascript"></Script><Scripttype= "Text/javascript"> //1. If the database exists then open does not exist then create and then open the varDB=Window.opendatabase ("phonegap100", "1.0","phonegap Chinese web",1024x768*1024x768* -); if(db) {Console.log ('Database Connection succeeded'); }Else{Console.log ('Database connection Failed'); } //2. Executing the transaction operations databaseDb.transaction (function(TX) {Tx.executesql ("CREATE TABLE Test (id int UNIQUE, name TEXT, age int)"); //Create a table }) //Inserting DataDb.transaction (function(TX) {//four parameters First SQL statement second SQL parameter third successful callback function fourth failed callback function //Tx.executesql ("INSERT into Test (id,name,age) VALUES (1, ' Zhang San ', 20)"); //Tx.executesql ("INSERT into Test (id,name,age) VALUES (2, ' John Doe ',") "); //Tx.executesql ("INSERT into Test (id,name,age) VALUES (?,?,?)", [3, ' Zhao Si ', 15],null,null);Tx.executesql ("INSERT INTO Test (Id,name,age) VALUES (3, ' Zhao Wu ', +)",[],function(Tx,rel) {if(rel.rowsaffected>0){ //New SuccessConsole.log ('add a piece of data successfully'); Console.log ('The ID of this piece of data'+Rel.insertid); } },function() {alert ('failed'); }); }); //Update DataDb.transaction (function(TX) {//Tx.executesql //four parameters First SQL statement second SQL parameter third successful callback function fourth failed callback function //Tx.executesql ("INSERT into Test (id,name,age) VALUES (1, ' Zhang San ', 20)"); //Tx.executesql ("INSERT into Test (id,name,age) VALUES (2, ' John Doe ',") "); //tx.executesql ("Update test set name=?,age=?") where id=1 ", [' Zhang 31 ', 50],null,null); //tx.executesql ("Update test set name= ' 322 ', age=60 where id=1"); }) //Delete DataDb.transaction (function(TX) {//Tx.executesql //four parameters First SQL statement second SQL parameter third successful callback function fourth failed callback function //tx.executesql ("Update test set name=?,age=?") where id=1 ", [' Zhang 31 ', 50],null,null); //tx.executesql ("Delete from Test where id=1"); }) //Querying DataDb.transaction (function(TX) {//Tx.executesql //four parameters First SQL statement second SQL parameter third successful callback function fourth failed callback functionTx.executesql ("SELECT * FROM Test",[],function(Tx,rel) { for(varI=0; I<Rel.rows.length;i++) {document.write ('Name:'+Rel.rows.item (i) ['name']+', Age:'+Rel.rows.item (i) [' Age']+'<br>'); } },function() {alert ('failed'); }); })</Script></Head> <Body></Body></HTML>
PhoneGap Storage Api_web SQL