HTML5 local database basic operation details, html5 database details
The following describes the APIs of the local database and their usage methods.
1. Use openDatabase to create a database
We can use the openDatabase method to create a database. The openDatabase method passes five parameters: Database Name, database version number (which can be omitted), description of the Database, size of the allocated database, and callback function.
To create a local database, run the following code:
Var myWebDatabase = openDatabase ("user", "1.0", "user info", 1024*1024, function (){});
In this way, a user information table is created. Then, you can verify whether the created local database is successful:
Copy content from SQL Code to clipboard
- If (! DataBase ){
- Alert ("The database has been created successfully !");
- } Else {
- Alert ("The database has not been successfully created .")
- } If (! DataBase ){
- Alert ("The database has been created successfully !");
- } Else {
- Alert ("The database has not been successfully created .")
- }
2. Execute SQL statements using the executeSql Method
With the executeSql method, we can directly execute normal SQL statements, as follows:
Context.exe cuteSql ('insert INTO testTable (id, name) VALUES (1, "Martin ")');
Of course, this only shows the functions of executeSql, and does not explain exactly how to use and use the executeSql method. To use this method, you must introduce transaction.
3. Use transaction to process transactions
This method is used to process transactions. Three parameters can be passed: A method containing the transaction content, a successful callback function, and a failed callback function (the latter two can be omitted ).
Combined with transaction and executeSql, we can add and create data tables to the database we created earlier. The Code is as follows:
Copy the content to the clipboard using JavaScript Code
- MyWebDatabase. transaction (function (context ){
- Context.exe cuteSql ('create table if not exists testTable (id unique, name )');
- Context.exe cuteSql ('insert INTO testTable (id, name) VALUES (0, "Byron ")');
- Context.exe cuteSql ('insert INTO testTable (id, name) VALUES (1, "Casper ")');
- Context.exe cuteSql ('insert INTO testTable (id, name) VALUES (2, "Frank ")');
- });
The meaning of SQL statements is not explained much, but here we can clearly see how to create a database data table and add data in a local database.
The above is all the content of this article, hoping to help you learn.
Link: http://blog.csdn.net/fareise/article/details/50786594