JavaScript authoritative Guide Learning Note 18: BOM New Achievements (1)--Client store data (Web SQL database implementation)

Source: Internet
Author: User

With local storage and session storage, simple object persistence can be achieved, with simple key-value pairs or objects stored. However, when dealing with more complex relational data, it is necessary to use Web SQL database. browser support for Web SQL database



First, how to use the Web SQL Database

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Results in Google and data in Web sql:


Second, asynchronous database

Use the Window.opendatabase () method to create a local database connection or to establish a connection to a database that already exists, called an asynchronous execution database. Because it is not a synchronous operation, it requires many callback functions to receive the results of asynchronous execution.


1, var db = Window.opendatabase (Name,version,displayname,estimatedsize[,creationcallback]): Returns a Database object.

Name: The database name is defined, case-sensitive and unique.

Version: the versions of the database that are created are unique.

DisplayName: A description of the database, typically used to describe the purpose of the database.

estimatedsize: The expected size of the database, in byte size, can be changed.

creationcallback: Optional, a callback function that executes when the database is not created.


2. Perform database transaction: Database creation is successfully returned to database object, which can be performed. The operation of the database is transactional.

2.1 db.transaction (Callback[,errorcallback[,sucesscallback]): Performs a read and write operation of the transaction.

CallBack: Defines the callback function to be executed by the transaction operation, in which the SQL operations performed are transactional or all succeed or all fail. The function has a SqlTransaction object parameter that defines the method that performs the SQL operation.

Errorcallback: Defines the function that is called when a transaction operation fails, and the function has a parameter to the SQLError object.

sucesscallback: defines a function that is called when a transaction operation succeeds , without parameters.

2.2 db. readtransaction (Callback[,errorcallback[,sucesscallback]): Performs a read operation on a transaction and cannot be written. Parameters are the same as 2.1.


3. Database Version Management

Db.changeversion (oldversion,newversion[,Callback[,errorcallback[,sucesscallback]]): Used to change the version of the database. The next three optional parameters are the same as 2.1. oldversion is the current version number, which is generally set to Database.version (this property returns the version number of the DB); NewVersion: The new version number, which is the string type.


4. Execute SQL statement: The SqlTransaction object defines the ExecuteSQL () method to perform the SQL operation.

Tx.executesql (Sql[,arg[,callback[,errorcallback]]):

SQL: SQL statement executed

ARG: Optional, define replace in parameterized query "?" An array of placeholders that can be ignored or null if no parameterized query is used.

Callback: Defines a callback function that executes when a SQL operation succeeds, and can be set to NULL if it is not defined. The callback function has two parameters: the first is the SqlTransaction object, and the second is the Sqlresult object, which represents the result of the execution.

errorcallback: defines a callback function that executes when a SQL operation fails, and can be set to NULL if it is not defined. The callback function has two parameters: the first is the SqlTransaction object, and the second is the SQLError object.



Third, synchronize the database

Synchronous database operations are used only within workers and cannot be used in Web pages, but asynchronous databases can be used within a worker. Currently, only Chrome 6 supports synchronizing databases.


1. Create a connection: Use the Workerglobalscope.opendatabasesync () method within the worker () to create a local database or establish a connection between an existing database. Called synchronous databases, there is no need for various callback functions to handle the results of database operations.

var db = self. Opendatabasesync (name,version,displayname,estimatedsize[,creationcallback]) : Returns a Databasesync object with the same OpenDatabase () method as the parameter.


2. Perform database transactions: Db.transaction (callback) [perform read and write operations] and db.readtransaction (callback) [Perform write], The argument to the callback function is a Sqltransactionsync object that defines the method that performs the SQL operation.

The JS code for the transaction operation is placed in a worker.js file//create worker in HTML and receive the message if (window. Worker) {var worker = new Worker ("Worker.js"); Else{alert ("Worker not Supported");}


3. Database Version Management :

Db.changeversion (oldversion,newversion[,callback]): Used to change the version of the database. The optional parameters are as above. oldversion is the current version number, which is generally set to Databasesync.version (this property returns the version number of the DB); NewVersion: The new version number, which is the string type.


4 . Execute SQL statement: The Sqltransactionsync object defines the ExecuteSQL () method to perform the SQL operation.

var re=tx.executesql (Sql[,arg]): return result set

SQL: SQL statement executed

ARG: Optional, define replace in parameterized query "?" An array of placeholder characters. If you do not use a parameterized query, the parameters can be ignored or empty.



Iv. processing the results of database operations

SQL executed successfully using the ExecuteSQL () method returns an execution result that is returned as a parameter to the ExecuteSQL () and is a Sqlresult object.

Interface sqlresult{readonly attribute long Insertid;//return record line of idreadonly attribute long rowsaffected;// Returns the number of rows changed by the SQL statement readonly Sttribute sqlresultrowlist rows;//return all record rows, no records return empty objects}
The Sqlresultrowlist object is represented bysqlresultsetrowlist Interface Definition

Interface sqlresultsetrowlist{readoonly attribute unsigned long length;//Put back the number of rows in the record getter any item (in unsigned long index); Gets the specified row according to the index, the index does not exist return null}


v. SQL injection

Use parameterized queries to prevent malicious SQL injection. A parameterized query specifies that a question mark (?) is used in a SQL statement. As a parameter placeholder, provides a secure, encapsulating method for database access to application development, handing input to the database for preprocessing.



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.