The Web SQL database API is not part of the HTML5 specification, but it is a separate specification that introduces a set of APIs that use SQL to manipulate client databases.
Core approach
opendatabase-Create a database object using an existing database or a new database
Transaction-can control a thing and execute commits or rollbacks based on this condition
executesql-executing the actual SQL statement
Open Database
// Open the existing database with the OpenDatabase () method and create a new database if the database does not exist var db = OpenDatabase ('mydb'1.0 ' Test DB'21024x768);
The OpenDatabase () method corresponds to five parameter descriptions:
- Database name
- Version number
- Description text
- Database Size (bytes)
- Create callback (optional)
The fifth argument, the creation callback is called after the database is created.
Create a table
var db = OpenDatabase ('mydb'1.0 ' Test DB'21024x768);d b.transaction (function (TX) { Tx.executesql ('CREATE TABLE IF not ' EXISTS LOGS (id unique, log)');});
Inserting data
vardb = OpenDatabase ('MyDB','1.0','Test DB',2*1024x768*1024x768);d b.transaction (function (TX) {Tx.executesql ('CREATE TABLE IF not EXISTS LOGS (id unique, log)'); Tx.executesql ('INSERT into LOGS (ID, log) VALUES (1, "blog Park")'); Tx.executesql ('INSERT into LOGS (ID, log) VALUES (2, "www.cnblogs.com")');});
You can also use dynamic values to insert data
//e_id and E_log are external variables, and ExecuteSQL maps each entry in the array parameter to "?" .
vardb = OpenDatabase ('MyDB','1.0','Test DB',2*1024x768*1024x768);d b.transaction (function (TX) {Tx.executesql ('CREATE TABLE IF not EXISTS LOGS (id unique, log)'); Tx.executesql ('INSERT into LOGS (id,log) VALUES (?,?)', [e_id, E_log]);});
Reading data
vardb = OpenDatabase ('MyDB','1.0','Test DB',2*1024x768*1024x768); Db.transaction (Function (TX) {Tx.executesql ('CREATE TABLE IF not EXISTS LOGS (id unique, log)'); Tx.executesql ('INSERT into LOGS (ID, log) VALUES (1, "blog Park")'); Tx.executesql ('INSERT into LOGS (ID, log) VALUES (2, "www.cnblogs.com")');}); Db.transaction (Function (TX) {Tx.executesql ('SELECT * from LOGS', [], function (TX, results) {varLen =results.rows.length; Msg="<p> Query record number:"+ len +"</p>"; Document.queryselector ('#status'). InnerHTML + =msg; for(i =0; i < Len; i++) {msg="<p><b>"+ Results.rows[i].log +"</b></p>"; } }, NULL);});
Deleting records
db.transaction (Function (TX) { tx.executesql ('DELETE from LOGS WHERE id=1' );});
Deleting data can also be dynamic
db.transaction (Function (TX) { tx.executesql ('DELETE from LOGS WHERE id=? ' , [id]);});
Update record
Tx.executesql ("UPDATE CC SET logname= ' www.baidu.com ' WHERE id=2");
The update data can also be dynamic
Tx.executesql ("UPDATE CC SET logname= ' www.baidu.com ' WHERE id=? ") ", [id]);
Note : Delete and modify can not and build table statements put in a executesql, it is best to write separately
The following is a built-in database
Html5-web SQL database