HTML5 Mobile Development Road (--HTML5) Web SQL Database

Source: Internet
Author: User
Tags web database



This paper is the official HTML5 training course for Brother Lian it education organization, mainly introduces: HTML5 Mobile Development Road (a)--HTML5 Web SQL Database



I. Introduction of WEB Database


 

websql database API is not actually part of the HTML5 specification, but a separate specification. It manipulates the client's database through a set of APIs. Safari, Chrome, Firefox, opera and other mainstream browsers have supported Websql database

websql databases have three core methods:

1) Open the Database OpenDatabase () method:


2) transaction Transaction () method:


3) Execute SQL command ExecuteSQL () method:



Second, WEB database operation use


1) Open the Database OpenDatabase () method:


This method creates a database object that can either use an existing database or create a new database.


2) transaction transaction () method:


This method can be used to control transactions, perform commit operations, or rollback operations.


3) Execute SQL command ExecuteSQL () method:


This method is used to execute SQL queries.


Third, examples



[HTML] View plain copy

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="urf-8"/>
    </head>
    <body>
        <script type="text/javascript">
            / / Create a database

            Var db = window.openDatabase("dawanganban", "1.0","Database Description", 20000);
            / / Create a data table
           Db.transaction(function(tx) {
                tx.executeSql("CREATE TABLE test (id int UNIQUE, mytitle TEXT, timestamp REAL)");
            });

            / / Insert data
            Db.transaction(function(tx) {
               tx.executeSql("INSERT INTO test (id, mytitle, timestamp) values(?, ?, ?)", [1, "WEB Database", new Date().getTime()], null, null);
            });

  

            / / Insert data
            Db.transaction(function(tx) {
                tx.executeSql("INSERT INTO test (id, mytitle, timestamp) values(?, ?, ?)", [2, "DaWanGanBan", new Date().getTime()], null, null);
            });

  

            //delete data  
            /*
            Db.transaction(function(tx){
                tx.executeSql("DELETE FROM test where mytitle=?",["WEB Database"],null,null);
            });

            */

              

            //db.transaction(function(tx) {
            // tx.executeSql("DROP TABLE qqs");
            //})

            //db.transaction(function(tx) {
            // tx.executeSql("update test set mytitle=? where mytitle = ‘fsafdsaf‘",[‘xp‘],null,null);
            //});

            //Query data  
            Db.transaction(function(tx) {
                tx.executeSql("SELECT * FROM test", [],
                Function(tx, result) {
                    For(var i = 0; i < result.rows.length; i++){
                        Document.write(‘<b>‘ + result.rows.item(i)[‘mytitle‘] + ‘</b><br />’);
                    }
                }, function(){
                    Alert("error");
                });
            });
        </script>
    </body>
</html>



HTML5 Mobile Development Road (--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.