• 中文(中华人民共和国)
    • English (United States)
    • العربية (مصر)
    • Deutsch (Deutschland)
    • Español (España, alfabetización internacional)
    • français (France)
    • हिंदी (भारत)
    • italiano (Italia)
    • 日本語 (日本)
    • 한국어 (대한민국)
    • Nederlands (Nederland)
    • polski (Polska)
    • русский (Россия)
    • ไทย (ไทย)
    • Türkçe (Türkiye)
    • Tiếng Việt (Việt Nam)
    • 中文(香港特別行政區)
  • 登录
  • 注册
DotNetAge - Mvc & jQuery CMS
隐藏边栏

HTML5 - Web SQL Database


The Web SQL Database API isn't actually part of the HTML5 specification but it is a separate specification which introduces a set of APIs to manipulate client-side databases using SQL.

I'm assuming you are a great web developer and if that is the case then no doubt, you would be well aware of SQL and RDBMS concepts. If you still want to have a session with SQL then, you can go through our SQL Tutorial.

Web SQL Database will work in latest version of Safari, Chrome and Opera.

The Core Methods:



There are following three core methods defined in the spec that I.m going to cover in this tutorial:
  1. openDatabase: This method creates the database object either using existing database or creating new one.
  2. transaction: This method give us the ability to control a transaction and performing either commit or rollback based on the situation.
  3. executeSql: This method is used to execute actual SQL query.

Opening Database:



The openDatabase method takes care of opening a database if it already exists, this method will create it if it already does not exist.

To create and open a database, use the following code:

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


Above method took following five paramters:

  1. Database name
  2. Version number
  3. Text description
  4. Size of database
  5. Creation callback

The last and 5th argument, creation callback will be called if the database is being created. Without this feature, however, the databases are still being created on the fly and correctly versioned.

Executing queries:



To execute a query you use the database.transaction() function. This function needs a single argument, which is a function that takes care of actually executing the query as follows:


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)');
});


The above query will create a table called LOGS in 'mydb' database.

INERT Operation:



To create enteries into the table we add simple SQL query in the above example as follows:


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, "foobar")');
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")');
});


We can pass dynamic values while creating entering as follows:


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];
});


Here e_id and e_log are external variables, and executeSql maps each item in the array argument to the "?"s.

READ Operation:



To read already existing records we use a callback to capture the results as follows:


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, "foobar")');
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")');
});

db.transaction(function (tx) {
tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {
var len = results.rows.length, i;
msg = "

Found rows: "

+ len + "";
document.querySelector('#status').innerHTML += msg;
for (i = 0; i < len; i++){
alert(results.rows.item(i).log );
}
}, null);
});


Final Example:



So finally, let us keep this example in full fledged HTML5 document as follows and try to run it with Safari browser.

DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
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, "foobar")');
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")');
msg = '

Log message created and row 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 = "

Found rows: "

+ len + "";
document.querySelector('#status').innerHTML += msg;
for (i = 0; i < len; i++){
msg = "

"

+ results.rows.item(i).log + "";
document.querySelector('#status').innerHTML += msg;
}
}, null);
});
script>
head>
<body>
<div id="status" name="status">Status Messagediv>
body>
html>


This would produce following result with latest version of either Safari or Opera:


Log message created and row inserted.
Found rows: 2
foobar
logmsg

 


    Average:
  • 阅读
    (3446)
  • (0)
  • 固定链接
上一篇:HTML5 Syntax
下一篇:HTML5 - Server Sent Events
分享至:

评论 (0)


  • rss
  • atom

对此文章尚没有发表任何评论

Tag cloud

Anything in here will be replaced on browsers that support the canvas element