Introduction
- openDatabase
Creates the database object, either using an existing database or creating a new one.
- transaction
Controls a transaction and performs either a commit or rollback on the basis of the situation.
- executeSql
Executes an SQL query.
Create/Open Web SQL Database
To create a Web SQL database, we can use a function called an open database, which has four parameters as follows:
- Database name
- Version Number
- Description
- Size
- Creation callback.
The last argument (Creation callback) will be called if the database is being created.
- var Database_Name = 'MyDatabase';
- var Version = 1.0;
- var Text_Description = 'My First Web-SQL Example';
- var Database_Size = 2 * 1024 * 1024;
- var dbObj = openDatabase(Database_Name, Version, Text_Description, Database_Size);
- dbObj.transaction(function (tx)
- {
- // do SQL here using the tx object
- });
- Table creation
- dbObj.transaction(function (tx)
- {
- tx.executeSql('CREATE TABLE IF NOT EXISTS Employee_Table (id unique, Name, Location,did)');
- tx.executeSql('CREATE TABLE IF NOT EXISTS dept_Table (did unique, dName,estd)');
- });
Note, if the database already exists, the transaction will fail. Thus, any successive SQL will not run. Thus, we can either use another transaction or we can only create the table if it doesn't exist. - Insert
Now, let's see, how to perform an insert operation.After insert
- var id = document.getElementById("tbID").value;
- var name = document.getElementById("tbName").value;
- var location = document.getElementById("tbLocation").value;
- var did = document.getElementById("tbLdept").value;
- dbObj.transaction(function (tx) {
- tx.executeSql('insert into Employee_Table(id, Name, Location,did) values(' + id + ',"' + name + '","' + location + '",' + did + ')');
- });

- Select
Now, let's see, how to retrieve the inserted record.
- dbObj.transaction(function (tx) {
- tx.executeSql('SELECT e.id,e.Name,e.Location,d.dName,d.did FROM Employee_Table e inner join dept_Table d on e.did=d.did ', [], function (tx, results) {
- var len = results.rows.length, i;
- // document.getElementById("tblGrid").innerHTML = '';
- $("#tblGrid").find("tr:gt(0)").remove();
- var str = '';
- for (i = 0; i < len; i++) {
- str += "<tr>";
- str += "<td>" + results.rows.item(i).id + "</td>";
- str += "<td>" + results.rows.item(i).Name + "</td>";
- str += "<td>" + results.rows.item(i).Location + "</td>";
- str += "<td>" + results.rows.item(i).dName + "</td>";
- str += "</tr>";
- document.getElementById("tblGrid").innerHTML += str;
- str = '';
- }
- Update
- var id = document.getElementById("ddlid").value;
- var name = document.getElementById("tbName").value;
- var location = document.getElementById("tbLocation").value;
- var did = document.getElementById("tbLdept").value;
- dbObj.transaction(function (tx) {
- tx.executeSql('update Employee_Table set Name="' + name + '",Location="' + location + '",did=' + did + ' where id=' + id + '');
- });
Now, let's see, how to perform an update operation. - Delete
Now, let's see, how to perform delete operation.
- var id = document.getElementById("ddlid").value;
- // alert(id);
- dbObj.transaction(function (tx) {
- tx.executeSql('delete from Employee_Table where id=' + id + '');
- });
- <!DOCTYPE html>
- <html>
- <head>
- <title>Open DataBase</title>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
- <script>
- var Database_Name = 'MyDatabase';
- var Version = 1.0;
- var Text_Description = 'My First Web-SQL Example';
- var Database_Size = 2 * 1024 * 1024;
- var dbObj = openDatabase(Database_Name, Version, Text_Description, Database_Size);
- dbObj.transaction(function (tx) {
- tx.executeSql('CREATE TABLE IF NOT EXISTS Employee_Table (id unique, Name, Location,did)');
- tx.executeSql('CREATE TABLE IF NOT EXISTS dept_Table (did unique, dName,estd)');
- var today = new Date();
- var dd = today.getDate();
- var mm = today.getMonth() + 1; //January is 0!
- var yyyy = today.getFullYear();
- if (dd < 10) {
- dd = '0' + dd
- }
- if (mm < 10) {
- mm = '0' + mm
- }
- var today = dd + '/' + mm + '/' + yyyy;
- tx.executeSql('insert into dept_Table(did, dName, estd) values(1,"IT","' + today + '")');
- tx.executeSql('insert into dept_Table(did, dName, estd) values(2,"Accountant","' + today + '")');
- tx.executeSql('insert into dept_Table(did, dName, estd) values(3,"Claerk","' + today + '")');
- tx.executeSql('insert into dept_Table(did, dName, estd) values(4,"Management","' + today + '")');
- alldetails();
- });
- function Insert() {
- var id = document.getElementById("tbID").value;
- var name = document.getElementById("tbName").value;
- var location = document.getElementById("tbLocation").value;
- var did = document.getElementById("tbLdept").value;
- dbObj.transaction(function (tx) {
- tx.executeSql('insert into Employee_Table(id, Name, Location,did) values(' + id + ',"' + name + '","' + location + '",' + did + ')');
- });
- alldetails();
- }
- function del() {
- var id = document.getElementById("ddlid").value;
- // alert(id);
- dbObj.transaction(function (tx) {
- tx.executeSql('delete from Employee_Table where id=' + id + '');
- });
- empidbind();
- alldetails();
- }
- function myFunction()
- {
- var id = document.getElementById("ddlid").value;
- dbObj.transaction(function (tx) {
- tx.executeSql('SELECT * from Employee_Table where id=' + id + '', [], function (tx, results)
- {
- document.getElementById("tbName").value = results.rows.item(0).Name;
- document.getElementById("tbLocation").value = results.rows.item(0).Location;
- document.getElementById("tbLdept").value = results.rows.item(0).did;
- }, null);
- });
- }
- function showdel() {
- empidbind();
- $('#tdorginal').hide();
- $('#tdid').show();
- $('#btnupdate').hide();
- $('#btnInsert').hide();
- $('#btndel').show();
- $('#btninsertshw').show();
- $('#btnupdateshw').show();
- $('#btndeleteshw').hide();
- ////////////
- $('#rowName').hide();
- $('#rowLocation').hide();
- $('#rowdept').hide();
- }
- function showin()
- {
- $('#tdid').hide();
- $('#tdorginal').show();
- $('#btnupdate').hide();
- $('#btnInsert').show();
- $('#btndel').hide();
- $('#btninsertshw').hide();
- $('#btnupdateshw').show();
- $('#btndeleteshw').show();
- ////////////
- $('#rowName').show();
- $('#rowLocation').show();
- $('#rowdept').show();
- /////////////
- document.getElementById("tbID").value='';
- document.getElementById("tbName").value='';
- document.getElementById("tbLocation").value='';
- document.getElementById("tbLdept").value='1';
- empidbind();
- }
- function empidbind()
- {
- dbObj.transaction(function (tx) {
- tx.executeSql('SELECT * from Employee_Table', [], function (tx, results) {
- var len = results.rows.length, i;
- document.getElementById("ddlid").innerHTML = '';
- var str = '';
- for (i = 0; i < len; i++) {
- str += "<option value=" + results.rows.item(i).id + ">" + results.rows.item(i).id + "</option>";
- document.getElementById("ddlid").innerHTML += str;
- str = '';
- }
- }, null);
- });
- }
- function showupdte()
- {
- empidbind();
- $('#tdorginal').hide();
- $('#tdid').show();
- $('#btnupdate').show();
- $('#btnInsert').hide();
- $('#btndel').hide();
- $('#btninsertshw').show();
- $('#btnupdateshw').hide();
- $('#btndeleteshw').show();
- $('#rowName').show();
- $('#rowLocation').show();
- $('#rowdept').show();
- }
- function updte() {
- var id = document.getElementById("ddlid").value;
- var name = document.getElementById("tbName").value;
- var location = document.getElementById("tbLocation").value;
- var did = document.getElementById("tbLdept").value;
- dbObj.transaction(function (tx) {
- tx.executeSql('update Employee_Table set Name="' + name + '",Location="' + location + '",did=' + did + ' where id=' + id + '');
- });
- alldetails();
- }
- function alldetails()
- {
- dbObj.transaction(function (tx) {
- tx.executeSql('SELECT e.id,e.Name,e.Location,d.dName,d.did FROM Employee_Table e inner join dept_Table d on e.did=d.did ', [], function (tx, results) {
- var len = results.rows.length, i;
- // document.getElementById("tblGrid").innerHTML = '';
- $("#tblGrid").find("tr:gt(0)").remove();
- var str = '';
- for (i = 0; i < len; i++) {
- str += "<tr>";
- str += "<td>" + results.rows.item(i).id + "</td>";
- str += "<td>" + results.rows.item(i).Name + "</td>";
- str += "<td>" + results.rows.item(i).Location + "</td>";
- str += "<td>" + results.rows.item(i).dName + "</td>";
- str += "</tr>";
- document.getElementById("tblGrid").innerHTML += str;
- str = '';
- }
- }, null);
- });
- }
- dbObj.transaction(function (tx) {
- tx.executeSql('SELECT * from dept_Table', [], function (tx, results) {
- var len = results.rows.length, i;
- var str = '';
- for (i = 0; i < len; i++) {
- str += "<option value=" + results.rows.item(i).did + ">" + results.rows.item(i).dName + "</option>";
- document.getElementById("tbLdept").innerHTML += str;
- str = '';
- }
- }, null);
- });
- </script>
- </head>
- <body>
- <p id="hh"></p>
- <form id="frm1">
- <table id="tblinsert">
- <tr>
- <td>ID:</td>
- <td id="tdorginal"><input type="text" id="tbID" /><span style="color:red">*ID must be unique</span></td>
- <td id="tdid" style="display:none">
- <select id="ddlid" onchange="myFunction()"></select>
- </td>
- </tr>
- <tr id="rowName">
- <td>Name:</td>
- <td><input type="text" id="tbName" /></td>
- </tr>
- <tr id="rowLocation">
- <td>Location:</td>
- <td><input type="text" id="tbLocation" /></td>
- </tr>
- <tr id="rowdept">
- <td>Dept:</td>
- <td>
- <select id="tbLdept"></select>
- </td>
- </tr>
- <tr>
- </tr>
- </table>
- </form>
- <br />
- <button id="btnInsert" onclick="Insert()" style="color:green;display:block">Save</button>
- <button id="btnupdate" onclick="updte()" style="color:blue;display:none">Update</button>
- <button id="btndel" onclick="del()" style="color:red;display:none">Delete</button>
- <br /><br />
- <button id="btnupdateshw" onclick="showupdte()" style="color:red">update Employee details</button>
- <button id="btndeleteshw" onclick="showdel()" style="color:blue">Delete Employee details</button>
- <button id="btninsertshw" onclick="showin()" style="color:green;display:none">save Employee details</button>
- <br /><br />
- <table id="tblGrid" cellpadding="10px" cellspacing="0" border="1">
- <tr style="background-color:black;color:white;font-size:18px;">
- <td >
- ID
- </td>
- <td >
- Name
- </td>
- <td >
- Location
- </td>
- <td >
- Department
- </td>
- </tr>
- </table>
- <br />
- </body>
- </html>
Conclusion
In this article, we studied CRUD Operations Using WebSQL In HTML5 And jQuery.

Hassan AssaadPosted Apr 9, 2021, 10:10 AM
How can i apply the WAT (write ahead of time) in webSQL?
Meenal BhoiPosted Jan 7, 2019, 4:26 AM
It really helped me and worked well
SubashPosted Aug 23, 2016, 12:31 AM
Nice article sir
Bhavik PatelPosted Aug 22, 2016, 10:46 PM
Nice article!
Prasanna MuraliPosted Aug 22, 2016, 10:17 AM
Nice one...
Humayun Kabir MamunPosted Aug 22, 2016, 8:00 AM
Nice...
Soumalya DasPosted Aug 22, 2016, 5:31 AM
Thanks to all
Gowtham KPosted Aug 22, 2016, 4:39 AM
Good One, Thanks for sharing:)
Ramesh PalaniappanPosted Aug 22, 2016, 1:45 AM
Good One
kalu singh raoPosted Aug 22, 2016, 1:26 AM
Nice