HTML 5 Web SQL Core Trident

Source: Internet
Author: User
Tags insert log sql query version

The "51CTO translation" Web SQL database API, which is not actually contained in the HTML 5 specification, is a stand-alone specification that introduces a set of APIs that use SQL to manipulate the client database. Assuming you're a good web developer, there's no doubt that you're familiar with the concepts of SQL and databases, and if you're unfamiliar with SQL, it's a good idea to learn about SQL-related tutorials before you continue reading this article.

The latest version of Chrome,safari and opera browsers support the Web SQL database.

Core approach

This article describes the three core methods defined in the specification:

1. OpenDatabase: This method creates a database object using an existing database or creating a new database.

2, Transaction: This method allows us to control the transaction commit or rollback according to the situation.

3, ExecuteSQL: This method is used to execute the real SQL query.

Open Database

The OpenDatabase method opens a database that already exists, and if the database does not exist, it can also create the database, and the syntax for creating and opening the database is as follows:

 
  
 
  1. var db = OpenDatabase (' mydb ', ' 1.0 ', ' Test db ', 2 * 1024 * 1024);

The OpenDatabase method above uses the following five parameters:

1. Database name (mydb)

2, Version number (1.0)

3. Description (Test DB)

4. Database Size (2*1024*1024)

5. Create callback

The last, the fifth parameter "create callback," is invoked when the database is created, but even without this parameter, the database can be created at run time.

Execute Query

The Execute query uses the Database.transaction () function, which requires only one argument, and the following is a real query:

 
  
 
  1. var db = OpenDatabase (' mydb ', ' 1.0 ', ' Test db ', 2 * 1024 * 1024);
  2. Db.transaction (Function (TX) {
  3. Tx.executesql (' CREATE TABLE IF not EXISTS LOGS (id unique, log) ');
  4. });

The query above will create a logs table in the "MyDB" database.

Insert operation

To insert a new record into the table, we added a simple SQL query to the query above, with the following modified statement:

 
  
 
  1. var db = OpenDatabase (' mydb ', ' 1.0 ', ' Test db ', 2 * 1024 * 1024);
  2. Db.transaction (Function (TX) {
  3. Tx.executesql (' CREATE TABLE IF not EXISTS LOGS (id unique, log) ');
  4. Tx.executesql (' INSERT into LOGS (ID, log) VALUES (1, "Foobar") ");
  5. Tx.executesql (' INSERT into LOGS (ID, log) VALUES (2, "logmsg") ");
  6. });

When inserting new records, we can also pass dynamic values, such as:

 
  
 
  1. var db = OpenDatabase (' mydb ', ' 1.0 ', ' Test db ', 2 * 1024 * 1024);
  2. Db.transaction (Function (TX) {
  3. Tx.executesql (' CREATE TABLE IF not EXISTS LOGS (id unique, log) ');
  4. Tx.executesql (' INSERT into LOGS
  5. (Id,log) VALUES (?,? '), [e_id, E_log];
  6. });

Here the e_id and E_log are external variables, ExecuteSQL map each item to "?" in the array argument.

Read operations

If you want to read a record that already exists, we use a callback to capture the result, as follows:

 
 
  1. var db = OpenDatabase (' mydb ', ' 1.0 ', ' Test db ', 2 * 1024 * 1024);
  2. Db.transaction (Function (TX) {
  3. Tx.executesql (' CREATE TABLE IF not EXISTS LOGS (id unique, log) ');
  4. Tx.executesql (' INSERT into LOGS (ID, log) VALUES (1, "Foobar") ");
  5. Tx.executesql (' INSERT into LOGS (ID, log) VALUES (2, "logmsg") ");
  6. });
  7. Db.transaction (Function (TX) {
  8. Tx.executesql (' SELECT * from LOGS ', [], function (TX, results) {
  9. var len = results.rows.length, I;
  10. msg = "

    found rows:" + len + "

    ";
  11. Document.queryselector (' #status '). InnerHTML + msg;
  12. for (i = 0; i < len; i++) {
  13. Alert (Results.rows.item (i). log);
  14. }
  15. }, NULL);
  16. });

A complete example

Finally, we present the previous story in a complete HTML 5 document and use the browser to parse the HTML 5 document.

 
 
  1. status message

Here is the output produced in the latest version of Safari or Opera browser.

 
  
 
  1. Log message created and row inserted.
  2. Found Rows:2
  3. Foobar
  4. Logmsg

Original link: http://www.tutorialspoint.com/html5/html5_web_sql.htm

"Edit Recommendation"



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.