Introduction

"Web SQL" feature is new in HTML5. Web SQL is an API, which helps the developers to do some database operations on the client-side, like creating a database, opening the transaction, creating the tables, inserting values to the tables, deleting the values and reading the data. If you need any other way to save some data on the client-side, you can use the storage mechanisms, introduced in HTML5.
Browser Support
A Web SQL database will work in the latest version of Safari, Chrome, and Opera.
Differents types of Methods are,
  1. openDatabase
    Creates the database object, either using an existing database or creating a new one.
  2. transaction
    Controls a transaction and performs either a commit or rollback on the basis of the situation.
  3. 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:
The last argument (Creation callback) will be called if the database is being created.
  1. var Database_Name = 'MyDatabase';
  2. var Version = 1.0;
  3. var Text_Description = 'My First Web-SQL Example';
  4. var Database_Size = 2 * 1024 * 1024;
  5. var dbObj = openDatabase(Database_Name, Version, Text_Description, Database_Size);
Creating transaction
To create a transaction we can use the following syntax. We can use the transaction method from our database instance.
  1. dbObj.transaction(function (tx)
  2. {
  3. // do SQL here using the tx object
  4. });
Execute Sql
The executeSql method is used for both reads and write statements. To execute a SQL query, we can use the following syntax: Example
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Open DataBase</title>
  5. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  6. <script>
  7. var Database_Name = 'MyDatabase';
  8. var Version = 1.0;
  9. var Text_Description = 'My First Web-SQL Example';
  10. var Database_Size = 2 * 1024 * 1024;
  11. var dbObj = openDatabase(Database_Name, Version, Text_Description, Database_Size);
  12. dbObj.transaction(function (tx) {
  13. tx.executeSql('CREATE TABLE IF NOT EXISTS Employee_Table (id unique, Name, Location,did)');
  14. tx.executeSql('CREATE TABLE IF NOT EXISTS dept_Table (did unique, dName,estd)');
  15. var today = new Date();
  16. var dd = today.getDate();
  17. var mm = today.getMonth() + 1; //January is 0!
  18. var yyyy = today.getFullYear();
  19. if (dd < 10) {
  20. dd = '0' + dd
  21. }
  22. if (mm < 10) {
  23. mm = '0' + mm
  24. }
  25. var today = dd + '/' + mm + '/' + yyyy;
  26. tx.executeSql('insert into dept_Table(did, dName, estd) values(1,"IT","' + today + '")');
  27. tx.executeSql('insert into dept_Table(did, dName, estd) values(2,"Accountant","' + today + '")');
  28. tx.executeSql('insert into dept_Table(did, dName, estd) values(3,"Claerk","' + today + '")');
  29. tx.executeSql('insert into dept_Table(did, dName, estd) values(4,"Management","' + today + '")');
  30. alldetails();
  31. });
  32. function Insert() {
  33. var id = document.getElementById("tbID").value;
  34. var name = document.getElementById("tbName").value;
  35. var location = document.getElementById("tbLocation").value;
  36. var did = document.getElementById("tbLdept").value;
  37. dbObj.transaction(function (tx) {
  38. tx.executeSql('insert into Employee_Table(id, Name, Location,did) values(' + id + ',"' + name + '","' + location + '",' + did + ')');
  39. });
  40. alldetails();
  41. }
  42. function del() {
  43. var id = document.getElementById("ddlid").value;
  44. // alert(id);
  45. dbObj.transaction(function (tx) {
  46. tx.executeSql('delete from Employee_Table where id=' + id + '');
  47. });
  48. empidbind();
  49. alldetails();
  50. }
  51. function myFunction()
  52. {
  53. var id = document.getElementById("ddlid").value;
  54. dbObj.transaction(function (tx) {
  55. tx.executeSql('SELECT * from Employee_Table where id=' + id + '', [], function (tx, results)
  56. {
  57. document.getElementById("tbName").value = results.rows.item(0).Name;
  58. document.getElementById("tbLocation").value = results.rows.item(0).Location;
  59. document.getElementById("tbLdept").value = results.rows.item(0).did;
  60. }, null);
  61. });
  62. }
  63. function showdel() {
  64. empidbind();
  65. $('#tdorginal').hide();
  66. $('#tdid').show();
  67. $('#btnupdate').hide();
  68. $('#btnInsert').hide();
  69. $('#btndel').show();
  70. $('#btninsertshw').show();
  71. $('#btnupdateshw').show();
  72. $('#btndeleteshw').hide();
  73. ////////////
  74. $('#rowName').hide();
  75. $('#rowLocation').hide();
  76. $('#rowdept').hide();
  77. }
  78. function showin()
  79. {
  80. $('#tdid').hide();
  81. $('#tdorginal').show();
  82. $('#btnupdate').hide();
  83. $('#btnInsert').show();
  84. $('#btndel').hide();
  85. $('#btninsertshw').hide();
  86. $('#btnupdateshw').show();
  87. $('#btndeleteshw').show();
  88. ////////////
  89. $('#rowName').show();
  90. $('#rowLocation').show();
  91. $('#rowdept').show();
  92. /////////////
  93. document.getElementById("tbID").value='';
  94. document.getElementById("tbName").value='';
  95. document.getElementById("tbLocation").value='';
  96. document.getElementById("tbLdept").value='1';
  97. empidbind();
  98. }
  99. function empidbind()
  100. {
  101. dbObj.transaction(function (tx) {
  102. tx.executeSql('SELECT * from Employee_Table', [], function (tx, results) {
  103. var len = results.rows.length, i;
  104. document.getElementById("ddlid").innerHTML = '';
  105. var str = '';
  106. for (i = 0; i < len; i++) {
  107. str += "<option value=" + results.rows.item(i).id + ">" + results.rows.item(i).id + "</option>";
  108. document.getElementById("ddlid").innerHTML += str;
  109. str = '';
  110. }
  111. }, null);
  112. });
  113. }
  114. function showupdte()
  115. {
  116. empidbind();
  117. $('#tdorginal').hide();
  118. $('#tdid').show();
  119. $('#btnupdate').show();
  120. $('#btnInsert').hide();
  121. $('#btndel').hide();
  122. $('#btninsertshw').show();
  123. $('#btnupdateshw').hide();
  124. $('#btndeleteshw').show();
  125. $('#rowName').show();
  126. $('#rowLocation').show();
  127. $('#rowdept').show();
  128. }
  129. function updte() {
  130. var id = document.getElementById("ddlid").value;
  131. var name = document.getElementById("tbName").value;
  132. var location = document.getElementById("tbLocation").value;
  133. var did = document.getElementById("tbLdept").value;
  134. dbObj.transaction(function (tx) {
  135. tx.executeSql('update Employee_Table set Name="' + name + '",Location="' + location + '",did=' + did + ' where id=' + id + '');
  136. });
  137. alldetails();
  138. }
  139. function alldetails()
  140. {
  141. dbObj.transaction(function (tx) {
  142. 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) {
  143. var len = results.rows.length, i;
  144. // document.getElementById("tblGrid").innerHTML = '';
  145. $("#tblGrid").find("tr:gt(0)").remove();
  146. var str = '';
  147. for (i = 0; i < len; i++) {
  148. str += "<tr>";
  149. str += "<td>" + results.rows.item(i).id + "</td>";
  150. str += "<td>" + results.rows.item(i).Name + "</td>";
  151. str += "<td>" + results.rows.item(i).Location + "</td>";
  152. str += "<td>" + results.rows.item(i).dName + "</td>";
  153. str += "</tr>";
  154. document.getElementById("tblGrid").innerHTML += str;
  155. str = '';
  156. }
  157. }, null);
  158. });
  159. }
  160. dbObj.transaction(function (tx) {
  161. tx.executeSql('SELECT * from dept_Table', [], function (tx, results) {
  162. var len = results.rows.length, i;
  163. var str = '';
  164. for (i = 0; i < len; i++) {
  165. str += "<option value=" + results.rows.item(i).did + ">" + results.rows.item(i).dName + "</option>";
  166. document.getElementById("tbLdept").innerHTML += str;
  167. str = '';
  168. }
  169. }, null);
  170. });
  171. </script>
  172. </head>
  173. <body>
  174. <p id="hh"></p>
  175. <form id="frm1">
  176. <table id="tblinsert">
  177. <tr>
  178. <td>ID:</td>
  179. <td id="tdorginal"><input type="text" id="tbID" /><span style="color:red">*ID must be unique</span></td>
  180. <td id="tdid" style="display:none">
  181. <select id="ddlid" onchange="myFunction()"></select>
  182. </td>
  183. </tr>
  184. <tr id="rowName">
  185. <td>Name:</td>
  186. <td><input type="text" id="tbName" /></td>
  187. </tr>
  188. <tr id="rowLocation">
  189. <td>Location:</td>
  190. <td><input type="text" id="tbLocation" /></td>
  191. </tr>
  192. <tr id="rowdept">
  193. <td>Dept:</td>
  194. <td>
  195. <select id="tbLdept"></select>
  196. </td>
  197. </tr>
  198. <tr>
  199. </tr>
  200. </table>
  201. </form>
  202. <br />
  203. <button id="btnInsert" onclick="Insert()" style="color:green;display:block">Save</button>
  204. <button id="btnupdate" onclick="updte()" style="color:blue;display:none">Update</button>
  205. <button id="btndel" onclick="del()" style="color:red;display:none">Delete</button>
  206. <br /><br />
  207. <button id="btnupdateshw" onclick="showupdte()" style="color:red">update Employee details</button>
  208. <button id="btndeleteshw" onclick="showdel()" style="color:blue">Delete Employee details</button>
  209. <button id="btninsertshw" onclick="showin()" style="color:green;display:none">save Employee details</button>
  210. <br /><br />
  211. <table id="tblGrid" cellpadding="10px" cellspacing="0" border="1">
  212. <tr style="background-color:black;color:white;font-size:18px;">
  213. <td >
  214. ID
  215. </td>
  216. <td >
  217. Name
  218. </td>
  219. <td >
  220. Location
  221. </td>
  222. <td >
  223. Department
  224. </td>
  225. </tr>
  226. </table>
  227. <br />
  228. </body>
  229. </html>

Conclusion

In this article, we studied CRUD Operations Using WebSQL In HTML5 And jQuery.