Comment: Use 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
1. After creating or opening the database, you can use the transaction API transaction. Each transaction is an atomic operation that operates the database and is not interrupted, thus avoiding data conflicts. The definition of transaction is:
void transaction(querysql, errorCallback, successCallback);
querysql: Transaction callback function, where SQL statements can be executed. (Required)
errorCallback: An error callback function. (Optional)
successCallback: executes the successful callback function. (Optional)
2. In the callback function querysql, SQL statements can be executed. The corresponding API function is executeSQL. The definition of executeSQL is:
void executeSql(sqlStatement, arguments, callback, errorCallback);
sqlStatement: SQL statement. (Required)
arguments: Are the parameters required by SQL statements based on the SQL statement? One-dimensional array arranged in sequence. (Optional)
callback: callback function. (Optional)
errorCallback: An error callback function. (Optional)
Web SQL Database Example
The following is an example to illustrate the basic usage 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:
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")');
});