Web SQL database, Chinese translation as a "local database" is a lightweight database that is running on the browser side with the HTML5 specification.
in HTML5, it greatly enriches the content that the client can store locally, adds many functions to save the data that must be saved on the server to the client locally, greatly improves the performance of the Web application, reduces the burden on the server, and brings the Web era back to the "client-oriented , the server for the Light "era. in this, a very important feature is the local storage function of the database. A database that can be accessed through the SQL language is built into the HTML5. In HTML4, the database can only be placed on the server side, only through the server to access the database, but in HTML5, you can access the built-in database as easily as the local file. Now, like this does not need to be stored on the server, called "SQLite" file-type SQL database has been widely used, so HTML5 also used this database as a local database.
HTML5 Database It contains three core methods:1. OpenDatabase: This method creates database object 2 using an existing database or a new database . Transaction: This method allows us to control transactions to commit or rollback 3.EXECUTESQL based on the situation : This method is used to perform SQL operations
The ExecuteSQL function has four parameters, the meanings of which are:
1) A string representing the query, using the SQL language is SQLite 3.6.19.
2) string data inserted into the query where the question mark is located.
3) The callback function executed upon success. Returns two parameters: FX and results of execution.
4) A callback function that executes when a failure occurs. Returns two parameters: FX and failed error messages
1. Open a database or create a database
// CREATE database-open with this database, none is new var database=opendatabase (' emp ', ' 1.0 ', ' Employee Profile Management ', 1024*1024,function() {}); if(!database) {// databse to determine whether to create or open a successful alert (' Create/Open failed ')}Else {alert (' Create/Open Success ')}
The five parameters in Opendatabasek are: database name, version number, description, database size, create callback. Creating a callback does not create a database either.
2. Create a table
function Creater_sql () { // -Create TABLE database.transaction (function(FX) { Fx.executesql ( "CREATE table if not exists Stu (S_name text,s_score int,s_golds INT)", [], function(fx,result) {alert (' Create successful ')},// return status--Success function( Fx,error) {alert (' creation failed ')}// return status--failed ) });
3. Inserting data
function Insert () { // -inserting data database.transaction (function(FX) { Fx.executesql ("insert into Stu (S_name,s_score,s_golds) VALUES (?,?,?)" , [Name,sec1,s_gold], // [' John Doe ', 1000,1], function() {alert (' Execute successful ')},// return status--Success function(fx,e) {alert (' failed '); alert (e)})// return status--failed }); }
4. Query data
function Select () {// query data database.transaction (function ( FX) {Fx.executesql ( select * from Stu " function (fx,result) {alert (' query succeeded ' );}) }); }
Result is the result set of the query, where the most important attribute of the-sqlresultsetrowlist type is rows of the dataset.
Rows has two properties: Length, item.
Length represents the total number of bars, with item (NUM), which can be accessed to a specific line
5. Delete
// Delete a data table Database.transaction (function (FX) { fx.executesql (' drop table stu '); }); // Delete database database.transaction (function (FX) { Fx.executesql (' drop database Stu '); });
HTML5 Web SQL