Home >Web Front-end >H5 Tutorial >HTML5 local database details

HTML5 local database details

黄舟
黄舟Original
2017-03-16 16:10:011418browse

For complex databases, HTML5Use local database for operations. This is a browser-side database. In the local database we can directly use JavaScript to create the database, and use SQLThe statement executes related database operations. The following introduces each APIAPI of the local database and how to use it.

1. Use openDatabase to create a database

We can use openDatabaseMethod to create database. openDatabaseThe method passes five parameters, namely: database name, database version number (can be omitted), description of the database, setting the size of the allocated database, Callback function .

If we want to create a local database, we can execute the following code:

var myWebDatabase = openDatabase(“user”, ”1.0”, “user info”, 1024*1024, function(){});

This creates a user information table. You can then verify whether the created local database is successful:

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. Use the executeSql method to execute the sql statement

Using the executeSql method, we can directly execute the normal sql statement, as follows:

context.executeSql('INSERT INTO testTable(id,name) VALUES (1,"Martin")');

Of course, this only reflects the functions of executeSql , and does not explain exactly how and where to 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 and can pass three parameters: including transaction content A method, a callback function for successful execution, and a callback function for failed execution (the latter two can be omitted).

Combining transaction and executeSql, we can create a data table and add data to the database we created before , the code is as follows:

myWebDatabase.transaction(function (context) {
           context.executeSql('CREATE TABLE IF NOT EXISTS testTable (id unique, name)');
           context.executeSql('INSERT INTO testTable (id, name) VALUES (0, "Byron")');
           context.executeSql('INSERT INTO testTable (id, name) VALUES (1, "Casper")');
           context.executeSql('INSERT INTO testTable (id, name) VALUES (2, "Frank")');
         });

sql The meaning of the statement is not much explained, but from here it can be clearly seen how to create database data in a local database as in a general database. table and add data.

The above is the detailed content of HTML5 local database details. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn