Diffrent operation in HTML5 Web SQL Database

This article describe about HTML5 Web SQL Database operation.
  • 2067

HTML5 Web SQL Database operation

Executing queries:

For execute query on the database use database .transaction() method. This method take only single argument, this method take care of actually executing the query.

var dab = openDatabase('empdb', '1.0', 'test DB', 2 * 1024 * 2048);
dab.transaction(function (txt) {
txt.executeSql('CREATE TABLE IF NOT EXISTS LOGIN (id unique, logs)');
});

Note: The declared query create table in the empdb database.

INSERT Operation:

For this insert row or entries into the table LOGIN

var dab = openDatabase('empdb', '1.0', 'test DB', 2 * 1024 * 2048);
dab.transaction(function (txt) {
txt.executeSql('CREATE TABLE IF NOT EXISTS LOGIN (id unique, logs)');
txt.executeSql('INSERT INTO LOGIN (id, logs) VALUES (1, "amrit")');
txt.executeSql('INSERT INTO LOGIN(id, logs) VALUES (2, "loginmessage")');
txt.executeSql('INSERT INTO LOGIN (id, logs) VALUES (3, "my message")');
});

READ Operation:

The below declared part of program is used  for the reading purpose. This code execute line by line and fetch the insert rows from the LOGIN table

var dab = openDatabase('empdb', '1.0', 'test DB', 2 * 1024 * 2048);
dab.transaction(function (txt) {
txt.executeSql('CREATE TABLE IF NOT EXISTS LOGIN (id unique, logs)');
txt.executeSql('INSERT INTO LOGIN (id, logs) VALUES (1, "amrit")');
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "loginmessage")');
txt.executeSql('INSERT INTO LOGIN (id, logs) VALUES (3, "my message")');
});
dab.transaction(function (txt) {
txt.executeSql('SELECT * FROM LOGIN', [], function (txt, results) {
var leng = results.rows.length, i;
masg = "<p>Found rows: " + leng + "</p>";
document.querySelector('#status').innerHTML += masg;
for (i = 0; i < leng; i++){
alert(results.rows.item(i).log );
}
}, null);
});

Further Readings

You may also want to read these related articles :

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.