Html5-web SQL database

Source: Internet
Author: User

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:

    1. Database name
    2. Version number
    3. Description text
    4. Database Size (bytes)
    5. 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

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.