HTML5 local database basic operation details, html5 database details

Source: Internet
Author: User

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
  1. If (! DataBase ){
  2. Alert ("The database has been created successfully !");
  3. } Else {
  4. Alert ("The database has not been successfully created .")
  5. } If (! DataBase ){
  6. Alert ("The database has been created successfully !");
  7. } Else {
  8. Alert ("The database has not been successfully created .")
  9. }

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
  1. MyWebDatabase. transaction (function (context ){
  2. Context.exe cuteSql ('create table if not exists testTable (id unique, name )');
  3. Context.exe cuteSql ('insert INTO testTable (id, name) VALUES (0, "Byron ")');
  4. Context.exe cuteSql ('insert INTO testTable (id, name) VALUES (1, "Casper ")');
  5. Context.exe cuteSql ('insert INTO testTable (id, name) VALUES (2, "Frank ")');
  6. });

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.