Home  >  Article  >  Web Front-end  >  html5 Web SQL Database transaction processing function transaction and executeSQL analysis_html5 tutorial skills

html5 Web SQL Database transaction processing function transaction and executeSQL analysis_html5 tutorial skills

WBOY
WBOYOriginal
2016-05-16 15:48:431977browse

1. After creating or opening the database, you can use the transaction API transaction. Each transaction acts as an atomic operation to operate the database and will not be interrupted, thus avoiding data conflicts. The definition of transaction is:

Copy the code
The code is as follows:

void transaction(querysql, errorCallback, successCallback);

querysql: transaction callback function, in which SQL statements can be executed. (required)

errorCallback: Error callback function. (optional)

successCallback: Execution success callback function. (optional)

2. In the callback function querysql, you can execute SQL statements. The corresponding API function is executeSQL. The definition of executeSQL is:

Copy code
The code is as follows:

void executeSql(sqlStatement, arguments, callback, errorCallback);

sqlStatement :SQL statement. (required)

arguments: The parameters required by the SQL statement are the ones in the sql statement? One-dimensional array arranged sequentially. (optional)

callback: callback function. (optional)

errorCallback: Error callback function. (optional)

Web SQL Database Example

The following uses an example 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. Sample code:

Copy code
The code is as follows:

var db = openDatabase(' fooDB', '1.0', 'fooDB', 2 * 1024);
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS foo (id unique, text)') ;
tx.executeSql('INSERT INTO foo (id, text) VALUES (1, "foobar")');
});
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn