Home > Web Front-end > H5 Tutorial > body text

HTML5 local database details

黄舟
Release: 2017-03-16 16:10:01
Original
1394 people have browsed it

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(){});
Copy after login

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.”)
}
Copy after login

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!