HTML5 Web SQL Database Summary

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 the client database.

If you are a Web back-end programmer, it should be easy to understand SQL operations.

Web SQL Database works in the latest version of Safari, Chrome, and Opera browsers.

Core approach

The following are the three core methods defined in the specification:

    1. OpenDatabase: This method creates a database object using an existing database or a newly created database.
    2. Transaction: This method allows us to control a transaction and execute commits or rollbacks based on this condition.
    3. ExecuteSQL: This method is used to execute the actual SQL query.
1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4     <MetaCharSet= "UTF-8">5     <title>Javascript</title>6     <styletype= "Text/css">7 8     </style>9 </Head>Ten <Body> One <DivID= "status"name= "status">Status information</Div> A </Body> - </HTML> - <Scripttype= "Text/javascript"> the     /* - *opendatabase: Create a Database object using an existing database or a new database - five parameters corresponding to the *opendatabase method description: 1 Database Name 2 version number 3 Description text 4 database size 5 Create callback (create callback will be called after database creation) -     * */ +     varDB=OpenDatabase ('MyDB', '1.0', 'Test DB', 2 * 1024x768 * 1024x768); -     varmsg; +     /* A * Transaction This method allows us to control a thing and execute commits or rollbacks based on this situation.  at * ExecuteSQL This method is used to execute the actual SQL query.  - * After statement execution, a table named logs is created in the MyDB database.  -     * */ - Db.transaction (function(TX) { - Tx.executesql ('CREATE TABLE IF not EXISTS LOGS1 (id unique, log)'); - Tx.executesql ('INSERT into LOGS1 (ID, log) VALUES (1, "Nihao1")');//Inserting Data in Tx.executesql ('INSERT into LOGS1 (ID, log) VALUES (2, "Nihao2")');//Inserting Data - msg= 'The <p> data sheet has been created and two data has been inserted. </p>'; to Document.queryselector ('#status'). InnerHTML=msg; +     }); -  the     /* * * How to read data that already exists in the database $     * */Panax Notoginseng Db.transaction (function(TX) { - Tx.executesql ('SELECT * from LOGS1', [], function(TX, results) { the             varLen=results.rows.length, I; + msg= "<p> Query record number:" +Len+ "</p>"; A Document.queryselector ('#status'). InnerHTML+=msg; the  +              for(i= 0; I<Len; I++){ - msg= "<p><b>" +Results.rows.item (i). Log+ "</b></p>"; $ Document.queryselector ('#status'). InnerHTML+=msg; $             } -         }, NULL); -     }); the </Script>

Effect:

HTML5 Web SQL Database Summary

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.