In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the basic operation of HTML5 local database". In the daily operation, I believe that many people have doubts about the basic operation of HTML5 local database. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "basic operation of HTML5 local database". Next, please follow the editor to study!
The following describes each API of the local database and how to use it.
1. Create a database by using openDatabase
We can use the openDatabase method to create a database. The openDatabase method passes five parameters, namely: the database name, the database version number (which can be omitted), the description of the database, the size of the allocated database, and the callback function.
If we want to create a local database, we can execute the following code:
Var myWebDatabase = openDatabase ("user", "1.0"," user info ", 1024024, function () {})
This creates a table of user information. You can then verify the success of the local database you created:
SQL Code copies content to the 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. Use executeSql method to execute sql statement.
Using the executeSql method, we can execute the normal sql statement directly, as follows:
Context.executeSql ('INSERT INTO testTable (id,name) VALUES (1, "Martin"))
Of course, this only reflects the function of executeSql, and does not specify exactly how and where the executeSql method is used. To use this method, you must introduce transaction.
3. Use transaction to deal with transactions
This method is used to handle transactions and can pass three parameters: a method that contains the contents of the transaction, a callback function that executes successfully, and a callback function that fails (the latter two can be omitted).
With transaction and executeSql, we can add create tables and data to the database we created earlier, as follows:
JavaScript Code copies content to the clipboard
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"))
});
The meaning of the sql statement is not explained much, but from here you can clearly see how to create database tables and add data in the local database, just like in the general database.
At this point, the study on the "basic operation of HTML5 local database" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.