Alan learns HTML5 's web SQL Database

Source: Internet
Author: User

Without knowing what to do, the Web SQL database specification is no longer maintained, but most browsers support it!

The Web SQL Database specification page has such a declaration

The three core methods defined in the Web SQL Database specification:
    1. OpenDatabase: This method uses an existing database or creates a new database to create a database object
    2. Transaction: This method allows us to control transaction commit or rollback based on the situation
    3. ExecuteSQL: This method is used to execute SQL queries
OpenDatabase:

We can use a simple statement like this to create or open a local database object

var db = OpenDatabase (' TestDB ', ' 1.0 ', ' Test db ', 2 * 1024 * 1024);

OpenDatabase receives five parameters:

    1. Database name
    2. Database version number
    3. Display Name
    4. The size, in bytes, of the database save data
    5. callback function (not required)

If a callback function is provided, the callback function calls the Changeversion () function, and the callback function sets the database version number to NULL, regardless of the version number given. If no callback function is provided, the database is created with the given version number.

Transaction

The transaction method is used to handle transactions, and the entire transaction is rolled back when a statement execution fails. Method has three parameters

    1. A method that contains the contents of a transaction
    2. Execute successful callback function (optional)
    3. Execute failure callback function (optional)
ExecuteSQL

The ExecuteSQL method is used to execute the SQL statement, returning the result, with four parameters of the method

    1. Query string
    2. parameter to replace question mark in query string
    3. Execute successful callback function (optional)
    4. Execute failure callback function (optional)

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4     <title>Web SQL Database</title>5 </Head>6 <Body>7     <Scripttype= "Text/javascript">8         varDB=OpenDatabase ('TestDB', '1.0', 'Test DB', 2 * 1024x768 * 1024x768);9         varmsg;Ten Db.transaction (function(context) { One Context.executesql ('CREATE TABLE IF not EXISTS testtable (ID unique, name)'); A Context.executesql ('INSERT into testtable (ID, name) VALUES (0, "Byron")'); - Context.executesql ('INSERT into testtable (ID, name) VALUES (1, "Casper")'); - Context.executesql ('INSERT into testtable (ID, name) VALUES (2, "Frank")'); the          }); -  - Db.transaction (function(context) { - Context.executesql ('SELECT * from TestTable', [], function(context, results) { +             varLen=results.rows.length, I; - Console.log ('Got'+Len+'rows.'); +                 for(i= 0; I<Len; I++){ A Console.log ('ID:'+Results.rows.item (i). ID); at Console.log ('Name:'+Results.rows.item (i). name); -             } -          }); -         }); -     </Script> - </Body> in </HTML>

Alan learns HTML5 's 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.