11.45. HTML5 Web SQL database

发布时间 :2024-02-22 23:00:05 UTC      

The Web SQL database API is not part of the HTML5 specification, but it is a separate specification that introduces a set of APIs that uses SQL to operate on client-side databases.

If you are a Web back-end programmer, it should be easy to understand the operation of SQL.

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

11.45.1. Core method

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

  1. openDatabase this method creates a database object using either an existing database or a newly created database

  2. transaction this method allows us to control a transaction and perform a commit or rollback based on this situation.

  3. executeSql this method is used to execute the actual SQL query

11.45.2. Open the database

We can use it. openDatabase() method to open an existing database, and if the database does not exist, a new database is created, using the following code:

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

openDatabase() the five parameters corresponding to the method describe:

  1. Database name

  2. Version number

  3. Description text

  4. Database size

  5. Create callback

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

11.45.3. Perform query operation

Perform operations to use database.transaction() function:

vardb=openDatabase('mydb','1.0','Test
DB',2\*1024\*1024);db.transaction(function(tx){tx.executeSql('CREATE
TABLE IF NOT EXISTS LOGS (id unique, log)');});

The above statement, when executed, creates a table named LOGS in the ‘mydb’ database.

11.45.4. Insert data

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

vardb=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, "Rookie Tutorial")');tx.executeSql('INSERT INTO LOGS
(id, log) VALUES (2, "www.runoob.com")');});

We can also use dynamic values to insert data:

vardb=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 instance e_id and e_log is an external variable executeSql each entry in the array parameter is mapped to “?”.

11.45.5. Read data

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

vardb=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, "Rookie 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){varlen=results.rows.length,i;msg="<p>Number of query records:"+len+"</p>";document.querySelector('#status').innerHTML+=msg;for(i=0;i<len;i++){alert(results.rows.item(i).log);}},null);});

11.45.6. Complete instance

Example

vardb=openDatabase('mydb','1.0','Test
DB',2\*1024\*1024);varmsg;db.transaction(function(tx){tx.executeSql('CREATE
TABLE IF NOT EXISTS LOGS (id unique, log)');tx.executeSql('INSERT INTO
LOGS (id, log) VALUES (1, "Rookie Tutorial")');tx.executeSql('INSERT INTO LOGS
(id, log) VALUES (2,
"www.runoob.com")');msg='<p>The data table has been created and two pieces of data have been inserted.</p>';document.querySelector('#status').innerHTML=msg;});db.transaction(function(tx){tx.executeSql('SELECT
\* FROM
LOGS',[],function(tx,results){varlen=results.rows.length,i;msg="<p>Number of query records:"+len+"</p>";document.querySelector('#status').innerHTML+=msg;for(i=0;i<len;i++){msg="<p><b>"+results.rows.item(i).log+"</b></p>";document.querySelector('#status').innerHTML+=msg;}},null);});

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

Image0

11.45.7. Delete record

The format used to delete records is as follows:

db.transaction(function (tx) {
    tx.executeSql('DELETE FROM LOGS  WHERE id=1');
});

Deleting the specified data id can also be dynamic:

db.transaction(function(tx) {
    tx.executeSql('DELETE FROM LOGS WHERE id=?', [id]);
});

11.45.8. Update record

The format used to update records is as follows:

db.transaction(function (tx) {
    tx.executeSql('UPDATE LOGS SET log=\'www.w3cschool.cc\' WHERE id=2');
});

Updating the specified data id can also be dynamic:

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

11.45.9. Complete instance

Example

vardb=openDatabase('mydb','1.0','Test
DB',2\*1024\*1024);varmsg;db.transaction(function(tx){tx.executeSql('CREATE
TABLE IF NOT EXISTS LOGS (id unique, log)');tx.executeSql('INSERT INTO
LOGS (id, log) VALUES (1, "Rookie Tutorial")');tx.executeSql('INSERT INTO LOGS
(id, log) VALUES (2,
"www.runoob.com")');msg='<p>The data table has been created and two pieces of data have been inserted.</p>';document.querySelector('#status').innerHTML=msg;});db.transaction(function(tx){tx.executeSql('DELETE
FROM LOGS WHERE id=1');msg='<p>delete id is 1
record.</p>';document.querySelector('#status').innerHTML=msg;});db.transaction(function(tx){tx.executeSql('UPDATE
LOGS SET log=\\'www.w3cschool.cc\\'WHERE id=2');msg='<p>Update id to 2
record.</p>';document.querySelector('#status').innerHTML=msg;});db.transaction(function(tx){tx.executeSql('SELECT
\* FROM
LOGS',[],function(tx,results){varlen=results.rows.length,i;msg="<p>Number of query records:"+len+"</p>";document.querySelector('#status').innerHTML+=msg;for(i=0;i<len;i++){msg="<p><b>"+results.rows.item(i).log+"</b></p>";document.querySelector('#status').innerHTML+=msg;}},null);});

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

Image1

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.