How to understand the transaction functions transaction and executeSQL in html5 Web SQL Database
This article mainly explains "how to understand the transaction functions transaction and executeSQL in html5 Web SQL Database". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand transaction and executeSQL in html5 Web SQL Database".
1. After you create or open the database, you can use the transaction API transaction. Each transaction, as an atomic operation that operates the database, is not interrupted, thus avoiding data conflicts. Transaction is defined as:
The code is as follows:
Void transaction (querysql, errorCallback, successCallback)
Querysql: the transaction callback function, where the SQL statement can be executed. (required)
ErrorCallback: error callback function. (optional)
SuccessCallback: execute the callback function successfully. (optional)
2. In the callback function querysql, you can execute the SQL statement. The corresponding API function is defined as executeSQL,executeSQL:
The code is as follows:
Void executeSql (sqlStatement, arguments, callback, errorCallback)
SqlStatement:SQL statement. (required)
The parameters required by the arguments:SQL statement are those in the sql statement. An one-dimensional array arranged sequentially. (optional)
Callback: callback function. (optional)
ErrorCallback: error callback function. (optional)
Web SQL Database example
Here is an example to illustrate the basic use of Web SQL Database. It first calls openDatabase to create a database called "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.executeSql ('CREATE TABLE IF NOT EXISTS foo (id unique, text)')
Tx.executeSql ('INSERT INTO foo (id, text) VALUES (1, "foobar"))
});
Thank you for reading, the above is the content of "how to understand the transaction functions transaction and executeSQL in html5 Web SQL Database". After the study of this article, I believe you have a deeper understanding of how to understand the transaction functions transaction and executeSQL in html5 Web SQL Database, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!