Web SQL Database
2013-12-19 19:12
337 查看
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.
There are following three core methods defined in the spec that I.m going to cover in this tutorial:
openDatabase: This method creates the database object either using existing database or creating new one.
transaction: This method give us the ability to control a transaction and performing either commit or rollback based on the situation.
executeSql: This method is used to execute actual SQL query.
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:
Above method took following five paramters:
Database name
Version number
Text description
Size of database
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.
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:
The above query will create a table called LOGS in 'mydb' database.
To create enteries into the table we add simple SQL query in the above example as follows:
We can pass dynamic values while creating entering as follows:
Here e_id and e_log are external variables, and executeSql maps each item in the array argument to the "?"s.
To read already existing records we use a callback to capture the results as follows:
So finally, let us keep this example in full fledged HTML5 document as follows and try to run it with Safari browser.
This would produce following result with latest version of either Safari or Opera:
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:openDatabase: This method creates the database object either using existing database or creating new one.
transaction: This method give us the ability to control a transaction and performing either commit or rollback based on the situation.
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); |
Database name
Version number
Text description
Size of database
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) { |
INSERT 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) { |
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);db.transaction(function (tx) { |
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) { |
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> |
Log message created and row inserted. Found rows: 2 foobar logmsg |
相关文章推荐
- web sql Database
- HTML5 Web SQL Database 数据库的使用方法
- HTML5 Web SQL Database 与 Indexed Database 的 CRUD 操作
- HTML5 Web SQL Database 数据库的使用方法【图文说明】
- html 5 本地数据库(Web Sql Database)核心方法
- HTML5 Web SQL Database 数据库的使用方法
- HTML5 Web Database 数据库的SQL语句的使用方法
- Web SQL Database
- HTML5开发学习:本地存储Web Sql Database
- HTML5本地存储——Web SQL Database
- The 'System.Web.Security.SqlRoleProvider' requires a database schema compatible with schema version '1'. 解决方法
- Web SQL Database
- Web Sql database 本地数据库
- Web SQL Database的异步机制
- HTML5本地存储——Web SQL Database
- Web SQL Database
- HTML5 Web SQL Database 与 Indexed Database 的 CRUD 操作
- Html5 web sql database
- html5 使用web SQL database简单实例
- html 5 本地数据库(二)-- Web Sql Database核心方法openDatabase、transaction、executeSql 详解