Parsing of transaction processing functions transaction and executeSQL in html5 Web SQL Database

Source: Internet
Author: User

Comments: An example is provided to illustrate the basic usage of Web SQL Database. It first calls openDatabase to create a database named "fooDB. Then, use transaction to execute two SQL statements. The first SQL statement creates a table named "foo", and the second SQL statement inserts a record into the table.

1. After creating or opening a database, you can use the transaction API transaction. Each transaction, as an atomic operation to operate the database, will not be interrupted, thus avoiding data conflicts. Transaction is defined:

The Code is as follows:
Void transaction (querysql, errorCallback, successCallback );

Querysql:The transaction callback function can execute SQL statements. (Required)

ErrorCallback:Error callback function. (Optional)

SuccessCallback:The callback function is successfully executed. (Optional)

2. In the callback function querysql, You can execute an SQL statement. The corresponding API function is executeSQL. The definition of executeSQL is as follows:

The Code is as follows:
Void executeSql (sqlStatement, arguments, callback, errorCallback );

SqlStatement:SQL statement. (Required)

Arguments:Which of the following parameters is required for an SQL statement? A one-dimensional array in sequence. (Optional)

Callback:Callback function. (Optional)

ErrorCallback:Error callback function. (Optional)

Web SQL Database example

The following example shows the basic usage of Web SQL Database. It first calls openDatabase to create a database named "fooDB. Then, use transaction to execute two SQL statements. The first SQL statement creates a table named "foo", and the second SQL statement inserts a record into the table. Sample Code:

The Code is as follows:
Var db = openDatabase ('foodb', '1'. 0', 'foodb', 2*1024 );
Db. transaction (function (tx ){
Tx.exe cuteSql ('create table if not exists foo (id unique, text )');
Tx.exe cuteSql ('insert INTO foo (id, text) VALUES (1, "foobar ")');
});


Related Article

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.