In order to use Web SQL, initially open the existing data base, if not create the new database. The code below is used to create or open the database.
[sql]
var db = openDatabase( ' mydb ' , ' 1.0 ' , ' Test DB ' , 2 * 1024 *1024);
[/sql]
The above code parameters are described below.
- Name of the database
- Version
- Description of text
- Database size
- Callback Creation
Now use the
transaction() function which is used to execute the queries and transaction function need only one argument code as shown below.
[sql]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, user)');
});[/sql]
Then enter the table data by adding simple queries to the code as shown below.
[sql]
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, user)');
tx.executeSql('INSERT INTO USER (id, name) VALUES (1, "foobar")');
tx.executeSql('INSERT INTO USER (id, name) VALUES (2, "logmsg")');
tx.executeSql('INSERT INTO USER (id, name) VALUES (3, "lomsg")');
[/sql]