1 Answer

0 votes
by

The Web SQL Database API isn't actually part of the HTML5 specification but it is a separate specification which introduces a set of APIs to manipulate client-side databases using SQL.

I'm assuming you are a great web developer and if that is the case then no doubt, you would be well aware of SQL and RDBMS concepts. If you still want to have a session with SQL then, you can go through our SQL Tutorial.

Web SQL Database will work in latest version of Safari, Chrome and Opera.

The Core Methods

There are following three core methods defined in the spec that I am going to cover in this tutorial −

  • openDatabase − This method creates the database object either using existing database or creating new one.

  • transaction − This method gives us the ability to control a transaction and performing either commit or rollback based on the situation.

  • executeSql − This method is used to execute actual SQL query.

Opening Database

The openDatabase method takes care of opening a database if it already exists, this method will create it if it already does not exist.

To create and open a database, use the following code −

var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);

The above method took the following five parameters −

  • Database name
  • Version number
  • Text description
  • Size of database
  • Creation callback

The last and 5th argument, creation callback will be called if the database is being created. Without this feature, however, the databases are still being created on the fly and correctly versioned.

Executing queries

To execute a query you use the database.transaction() function. This function needs a single argument, which is a function that takes care of actually executing the query as follows −

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, log)'); 
});

The above query will create a table called LOGS in 'mydb' database.

INSERT Operation

To create enteries into the table we add simple SQL query in the above example as follows −

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, log)'); 
   tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "foobar")'); 
   tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, 

Related questions

0 votes
    What is one advantage that HTML5 APIs offer for modern Web design? (1)They enable users to view Flash content on ... (4)They enable users to view multimedia without plug-ins...
asked Apr 21, 2021 in Technology by JackTerrance
0 votes
    Which of the following is correct about web workers in HTML5? A - Web Workers do all the computationally expensive tasks ... keep the page responsive. D - All of the above....
asked Dec 3, 2020 in Technology by JackTerrance
0 votes
    Which of the following is correct about Web form 2.0 in HTML5? A - Web Forms 2.0 is an extension to the forms ... styling that was required in HTML4. D - All of the above....
asked Dec 1, 2020 in Technology by JackTerrance
0 votes
    I have some app ideas that I want to release for free (with ads). I am a web developer that ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Discuss the different database types in SQL Azure?...
asked Dec 28, 2020 in Technology by JackTerrance
+1 vote
    SQL Azure is a cloud based relational database that is based on ________....
asked Oct 20, 2020 in Technology by JackTerrance
+1 vote
    SQL Azure is a cloud-based relational database service that is based on _________....
asked Oct 20, 2020 in Technology by JackTerrance
0 votes
    I have batch of user accounts and every user account's password is not crypted.I want to insert to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I have a self hosted gitlab on ubuntu machine. I configure a linux container for it to run runner. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    I've got a local .mdf SQL database file that I am using for an integration testing project. Everything ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I try to create database from entity framework code first follow with this tutorial http://www.asp.net/mvc ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I'm currently developing a web application using asp.net c#. I got a server where I today was ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    I use the query below: update ACCOUNT_EXTERNAL_IDS set EXTERNAL_ID = 'username:vietnt' where ACCOUNT_ID='1000000' ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    I am writing a powershell script that will automate a dev environment deployment and I've hit a problem ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    I've created a BACPAC backup of my Azure SQZ database using the option "Export" in Azure management console. I ... written it already . Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
...