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 ")');
});