These two days to understand the next Web SQL datebase, I think it is necessary to mark, although it has been suspended research and development, but I believe that in the future will be a great development. In this only mark under its basic usage, for later use!
Web SQL database, Chinese translation as a "local database" is a lightweight database that is running on the browser side with the HTML5 specification. The shopping cart for a shopping site, for example, stores simple key-value pairs or simple objects, but cannot deal with tedious relational data.
The three core methods defined in the Web SQL Database specification:
1.openDatabase: This method uses an existing database or creates a new database to create a database object;
2.transaction: This method allows us to control transaction commit or rollback according to the situation;
3.EXECUTESQL: This method is used to execute SQL query 4 parameters of the SQL statement to be executed by passing the arguments into the SQL statement.
Establish a database called student, which has a version of 1.0 and a description of Test DB with a size of 1M bytes. The OpenDatabase method opens a database that already exists, and if the database does not exist, create the database with the following code:
<script type= "Text/javascript" >
var database=opendatabase (' Student ', ' 1.0 ', ' Employee Profile Management ', 1024*1024,function () {});
if (!database) {
Alert (' Create/Open failed ')
}else{
Alert (' Create/Open success ')
}
</script>
The OpenDatabase method uses five parameters:
1) database name
2) Version number
3) Description
4) Database size
5) Create callback function
To create a table:
<script type= "Text/javascript" >
var database=opendatabase (' emp ', ' 1.0 ', ' Employee Profile Management ', 1024*1024,function () {});
Database.transaction (function (FX) {
Fx.executesql (
"CREATE table if not exists Stu (s_id REAL unique,s_name TEXT)",
[],
function (Fx,result) {alert (' Create successful ')}
function (fx,error) {alert (' Create failed '}
)
})
</script>
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.
What's new in the table:
<script type= "Text/javascript" >
var database=opendatabase (' emp ', ' 1.0 ', ' Employee Profile Management ', 1024*1024,function () {});
Database.transaction (function (FX) {
Fx.executesql ("INSERT into Emps (id,name) VALUES (?,?)",
[1, ' Xiao Ming '],
function () {Alert (' Execution succeeded ')},
function () {alert (' failed ')})
})
</script>
Inquire
var database=opendatabase (' emp ', ' 1.0 ', ' Employee Profile Management ', 1024*1024,function () {});
Database.transaction (function (FX) {
Fx.executesql ("SELECT * from Emps",
[],
function (Fx,result) {
Alert (result.rows.length)
},
function () {alert (' failed ')})
})
>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
<script type= "Text/javascript" >
var database=opendatabase (' emp ', ' 1.0 ', ' Employee Profile Management ', 1024*1024,function () {});
Database.transaction (function (FX) {
Fx.executesql ("SELECT * from Emps",
[],
function (Fx,result) {
Alert (Result.rows.item (0). ID)
},
function () {alert (' failed ')})
})
</script>
To delete a data table:
Database.transaction (function (FX) {
Fx.executesql (' drop table Stu ');
});
To delete a database:
Database.transaction (function (FX) {
Fx.executesql (' drop database Stu ');
});
Basic usage of Web SQL Database