Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML5 Web SQL Database

HTML5 Web SQL Database

Status of Web SQL:The Web SQL API has been deprecated and is no longer recommended. Newer browser standards prefer IndexedDB for client-side storage. Web SQL is still supported only in some older browsers.

Alternative: Due to the uncertainty of Web SQL's future and the lack of broad support, it is recommended to switch to IndexedDB, a modern browser API that supports transactions and key-value storage.

The Web SQL Database API is not part of the HTML5 specification, but it is a separate specification that introduces a set of APIs for operating client-side databases using SQL.

If you are a web back-end programmer, you should find SQL operations easy to understand.

You can also refer to our SQL tutorial, to learn more about database operations.

Web SQL Database works in the latest versions of Safari, Chrome, and Opera browsers.


Core Methods

The following are the three core methods defined in the specification:

  1. openDatabase: This method creates a database object using an existing database or a newly created one.
  2. transaction: This method allows us to control a transaction and perform a commit or rollback based on the situation.
  3. executeSql: This method is used to execute actual SQL queries.

Opening a Database

We can use the openDatabase() method to open an existing database; if the database does not exist, a new one will be created. The code is as follows:



var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);

Description of the five parameters corresponding to the openDatabase() method:

  1. Database names
  2. Version number
  3. Description text
  4. Database size
  5. Creation callback

The fifth parameter, the creation callback, is called after the database is created.


Executing Query Operations

Operations are executed using the database.transaction() function:

var

db

=

openDatabase

(

'

mydb

'

,

'

1.0

'

,

'

Test DB

'

,

2

1024

1024

)

;

db

.

transaction

(

function

(

tx

)

{

tx

.

executeSql

(

'

CREATE TABLE IF NOT EXISTS LOGS (id unique, log)

'

)

;

}

)

;

After the above statement is executed, a table named LOGS will be created in the 'mydb' database.


Insert data

After executing the above create-table statement, we can insert some data:

var

db

=

openDatabase

(

'

mydb

'

,

'

1.0

'

,

'

Test DB

'

,

2

1024

1024

)

;

db

.

transaction

(

function

(

tx

)

{

tx

.

executeSql

(

'

CREATE TABLE IF NOT EXISTS LOGS (id unique, log)

'

)

;

tx

.

executeSql

(

'

INSERT INTO LOGS (id, log) VALUES (1, "Runoob Tutorial")

'

)

;

tx

.

executeSql

(

'

INSERT INTO LOGS (id, log) VALUES (2, "www.runoob.com")

'

)

;

}

)

;

We can also use dynamic values to insert data:

var

db

=

openDatabase

(

'

mydb

'

,

'

1.0

'

,

'

Test DB

'

,

2

1024

1024

)

;

db

.

transaction

(

function

(

tx

)

{

tx

.

executeSql

(

'

CREATE TABLE IF NOT EXISTS LOGS (id unique, log)

'

)

;

tx

.

executeSql

(

'

INSERT INTO LOGS (id,log) VALUES (?, ?)

'

,

[

e_id

,

e_log

]

)

;

}

)

;

In the example, e_id and e_log are external variables, and executeSql maps each entry in the array parameter to "?".


Read data

The following example demonstrates how to read data that already exists in the database:

var

db

=

openDatabase

(

'

mydb

'

,

'

1.0

'

,

'

Test DB

'

,

2

1024

1024

)

;

db

.

transaction

(

function

(

tx

)

{

tx

.

executeSql

(

'

CREATE TABLE IF NOT EXISTS LOGS (id unique, log)

'

)

;

tx

.

executeSql

(

'

INSERT INTO LOGS (id, log) VALUES (1, "Runoob Tutorial")

'

)

;

tx

.

executeSql

(

'

INSERT INTO LOGS (id, log) VALUES (2, "www.runoob.com")

'

)

;

}

)

;

db

.

transaction

(

function

(

tx

)

{

tx

.

executeSql

(

'

SELECT * FROM LOGS

'

,

[

]

,

function

(

tx

,

results

)

{

var

len

=

results

.

rows

.

length

,

i

;

msg

=

"

Query the number of records:

"

len

"

"

;

document

.

querySelector

(

'

#status

'

)

.

innerHTML

+=

msg

;

for

(

i

=

0

;

i

<

len

;

i

++

)

{

alert

(

results

.

rows

.

item

(

i

)

.

log

)

;

}

}

,

null

)

;

}

)

;


Complete example

Example

var

db

=

openDatabase

(

'

mydb

'

,

'

1.0

'

,

'

Test DB

'

,

2

1024

1024

)

;

var

msg

;

db

.

transaction

(

function

(

tx

)

{

tx

.

executeSql

(

'

CREATE TABLE IF NOT EXISTS LOGS (id unique, log)

'

)

;

tx

.

executeSql

(

'

INSERT INTO LOGS (id, log) VALUES (1, "Runoob Tutorial")

'

)

;

tx

.

executeSql

(

'

INSERT INTO LOGS (id, log) VALUES (2, "www.runoob.com")

'

)

;

msg

=

'

The data table has been created and two records have been inserted.

'

;

document

.

querySelector

(

'

#status

'

)

.

innerHTML

=

msg

;

}

)

;

db

.

transaction

(

function

(

tx

)

{

tx

.

executeSql

(

'

SELECT * FROM LOGS

'

,

[

]

,

function

(

tx

,

results

)

{

var

len

=

results

.

rows

.

length

,

i

;

msg

=

"

Query the number of records:

"

len

"

"

;

document

.

querySelector

(

'

#status

'

)

.

innerHTML

+=

msg

;

for

(

i

=

0

;

i

<

len

;

i

++

)

{

msg

=

"

"

results

.

rows

.

item

(

i

)

.

log

"

"

;

document

.

querySelector

(

'

#status

'

)

.

innerHTML

+=

msg

;

}

}

,

null

)

;

}

)

;

Try it »

The result of the above example is shown in the following figure:


Delete records

The format for deleting records is as follows:



db.transaction(function (tx) {

    tx.executeSql('DELETE FROM LOGS  WHERE id=1');

});

The id of the data to delete can also be dynamic:



db.transaction(function(tx) {

    tx.executeSql('DELETE FROM LOGS WHERE id=?', [id]);

});


Updating Records

The format for updating records is as follows:



db.transaction(function (tx) {

    tx.executeSql('UPDATE LOGS SET log=\'www.w3cschool.cc\' WHERE id=2');

});

The id of the data to update can also be dynamic:



db.transaction(function(tx) {

    tx.executeSql('UPDATE LOGS SET log=\'www.w3cschool.cc\' WHERE id=?', [id]);

});


Complete example

Example

var

db

=

openDatabase

(

'

mydb

'

,

'

1.0

'

,

'

Test DB

'

,

2

1024

1024

)

;

var

msg

;

db

.

transaction

(

function

(

tx

)

{

tx

.

executeSql

(

'

CREATE TABLE IF NOT EXISTS LOGS (id unique, log)

'

)

;

tx

.

executeSql

(

'

INSERT INTO LOGS (id, log) VALUES (1, "Runoob Tutorial")

'

)

;

tx

.

executeSql

(

'

INSERT INTO LOGS (id, log) VALUES (2, "www.runoob.com")

'

)

;

msg

=

'

The data table has been created and two records have been inserted.

'

;

document

.

querySelector

(

'

#status

'

)

.

innerHTML

=

msg

;

}

)

;

db

.

transaction

(

function

(

tx

)

{

tx

.

executeSql

(

'

DELETE FROM LOGS WHERE id=1

'

)

;

msg

=

'

Delete the record with id 1.

'

;

document

.

querySelector

(

'

#status

'

)

.

innerHTML

=

msg

;

}

)

;

db

.

transaction

(

function

(

tx

)

{

tx

.

executeSql

(

'

UPDATE LOGS SET log=

'

www.w3cschool.cc

'

WHERE id=2

'

)

;

msg

=

'

Update the record with id 2.

'

;

document

.

querySelector

(

'

#status

'

)

.

innerHTML

=

msg

;

}

)

;

db

.

transaction

(

function

(

tx

)

{

tx

.

executeSql

(

'

SELECT * FROM LOGS

'

,

[

]

,

function

(

tx

,

results

)

{

var

len

=

results

.

rows

.

length

,

i

;

msg

=

"

Query the number of records:

"

len

"

"

;

document

.

querySelector

(

'

#status

'

)

.

innerHTML

+=

msg

;

for

(

i

=

0

;

i

<

len

;

i

++

)

{

msg

=

"

"

results

.

rows

.

item

(

i

)

.

log

"

"

;

document

.

querySelector

(

'

#status

'

)

.

innerHTML

+=

msg

;

}

}

,

null

)

;

}

)

;

Try it »

The result of the above example is shown in the following figure: