HTML5 initial test web SQL database

Source: Internet
Author: User

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 databaseThis type of data storage is suitable.

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.exe cutesql ("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.exe cutesql ("select * From todo", [],
Function(TX, result ){
For(VaRI = 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.

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.