Html5 Web SQL Database, html5database
The Web SQL Database API is not included in the html5 specification. It is an independent specification and introduces an API that uses SQL to operate the client Database. All modern browsers support this API. The SQL statement is SQLite.
In html5, Local and session storage is very convenient for Local storage. Although key-value pairs are easy to use, they cannot process a large amount of data structures, web SQL Database is suitable for this type of data storage.
Web SQL databases are asynchronous.
Connect to/create a Database :?
db = window.openDatabase( "db" , "1.0" , "aid database " ,1024); // Create a database connection named db |
OpenDatabase has five parameters: Database Name, version number, description, size, and callback function (which can be omitted). When a database is created during the first call, a connection is established. The Byte size can be set flexibly. It is best to set it to large enough. Yes (! Db) Test the connection. if (db) is not recommended because the connection fails.
Query data:
db.transaction(function(tx) {
tx.executeSql("SELECT COUNT(*) FROM ToDo", [], function(result){}, function(tx, error){});
});
The transaction.exe cuteSql method is used to operate databases. The first parameter is an SQL statement. Both Data Reading and writing are performed here. For example:
db.transaction(function(tx) {
Tx.exe cuteSql ('create TABLE test1 (name int, value TEXT) '); // CREATE a TABLE
Tx.exe cuteSql ('insert INTO test1 (name, value) VALUES ("jack", "18"); // INSERT data
});
The second parameter is to replace the array, that is, the SQL statement can insert dynamic values. For example, the preceding line can be written
Db. transaction (function (tx ){
Tx.exe cuteSql ('insert INTO test1 (name, value) VALUES (?,? ) ', [N, v]); // insert data n, v
});
Both n and v are javascript variables. when inserting them, replace them with SQL statements?
The following functions are callback functions that are successful or fail to be executed.
Let's look at another example.
db.transaction(
function(tx) {
tx.executeSql("SELECT * FROM ToDo", [],
function(tx, result) {
for(var i = 0; i < result.rows.length; i++) {
document.write('<b>' + result.rows.item(i)['label'] + '</b><br />');
}
}, null);
}
);
Rows is a SQLResultSetRowList object that represents the rows returned by the database in sequence. If no row is returned, this object is null.