Html5 Web SQL Database, html5database

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.