WebSQL is a structured relational database at the client browser side. It’s a local RDBMS inside the browser on which you can fire SQL queries.
Is WebSQL a part of HTML 5 specification?
No, many people label it as HTML 5 but it’s not part of HTML 5 specification. The specification is based around SQLite.
So how can we use WebSQL ?
The first step we need to do is open the database by using “OpenDatabase” function as shown below. The first argument is the name of the database, the next is the version, then a simple textual title and finally the size of the database.
var db=openDatabase(‘dbCustomer’,’1.0′,’Customer app’, 2 * 1024 * 1024);
To execute SQL we then need to use “transaction” function and call “executeSql” function to fire SQL.
db.transaction(function (tx)
{
tx.executeSql(‘CREATE TABLE IF NOT EXISTS tblCust(id unique, customername)’);
tx.executeSql(‘INSERT INTO tblcust (id, customername) VALUES(1, “shiv”)’);
tx.executeSql(‘INSERT INTO tblcust (id, customername) VALUES (2, “raju”)’);
}
In case you are firing “select” query you will get data is “results” collection which we can loop and display in the HTML UI.
db.transaction(function (tx)
{
tx.executeSql(‘SELECT * FROM tblcust’, [], function (tx, results) {
for (i = 0; i < len; i++)
{
msg = “<p><b>” + results.rows.item(i).log + “</b></p>”;
document.querySelector(‘#customer).innerHTML += msg;
}
}, null);
});
Recently one of our readers was asked the below ASP.NET HTML 5 interview questions on server side events.
In case you are preparing for .NET and c# interview’s do see the below video which discusses what kind of questions are asked in the interviews.