Sibeesh Passion

Top Menu

  • Home
  • Search
  • About
  • Privacy Policy

Main Menu

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Home
  • Search
  • About
  • Privacy Policy

logo

Sibeesh Passion

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins

  • Post Messages to Microsoft Teams Using Python

  • Get Azure Blob Storage Blob Metadata Using PowerShell

  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines

CodeProjectHow to
Home›CodeProject›Introduction to Web SQL

Introduction to Web SQL

By SibeeshVenu
February 1, 2016
1210
0
Share:
Getting_version_details_from_database_instance

In this post we will see some informations about Web SQL. I know you all are familiar with SQL, If not I strongly recommend you to read some basic informations here . As the name implies, Web SQL has so many similarities with SQL. So if you are good in SQL, you will love Web SQL too. Web SQL is an API which helps the developers to do some database operations in client side, like creating database, open the transaction, creating tables, inserting values to tables, deleting values, reading the data. If you need any other way to save some data in client side, you can use storage mechanisms introduced in HTML5.
Now we will look some of the operations a developer can do with Web SQL. I hope you will like this.

Using the code

As you all know, to work with SQL queries, you must create a database. So the first step we are going to do, is creating the database.

Create/Open Web SQL Database

To create a Web SQL database, we can use a function called openDatabase which has four parameters as follows.

  • Database name
  • Version Number
  • Description
  • Size
  • Creation callback.
  • The creation callback gets fired while the database is being created.

    Now shall we open a Web SQL database with the above mentioned parameters? We can do that by running a query as follows.

    [sql]
    var myDBInstance = openDatabase(‘dbsibeeshpassion’, ‘1.0’, ‘This is a client side database’, 2 * 1024 * 1024);
    [/sql]

    Here I have given the size of my database as 2*1024*1024. In most browsers the size is flexible, but few maintains a limit of 5 MB. As from the above query we have created a Web SQL database. Now we will check whether the DB is creatd successfully or not.

    [js]
    //check whether the database is created or not.
    if (!myDBInstance) {
    alert(‘Oops, your database was not created’);
    }
    else {
    var version = myDBInstance.version;
    }
    [/js]

    Here, you will get an alert if the database is not created. Or you will be able to fetch the version details from the database instance.

    Getting_version_details_from_database_instance

    Getting_version_details_from_database_instance

    Once the database is created, we can start using the transaction as we uses in SQL.

    Creating transaction

    To create a transaction we can use the following syntax. We can use transaction method from our database instance.

    [js]
    myDBInstance.transaction(function (tran) {
    });
    [/js]

    Here myDBInstance is our database instance. And tran is our transaction object which we are going to use for our upcoming operations. Why we uses transaction is, as you all know transaction can be roll backed. For example, if any of the operation throws any error, the transaction will be roll backed so there won’t be any kind of mismatching data happening. And off course, we can easily manage error logs with the help of transaction. Shall we write queries needed for our operations?

    First of all, we will create a table in our database. To execute any queries in Web SQL, you must use the method executesql.

    [sql]
    tran.executeSql(‘CREATE TABLE IF NOT EXISTS Users (id unique, Name, MailID)’);
    [/sql]

    As you can see we are creating the table Users if it does not exists in the database. As in SQL we are assigning id as a unique key.

    Next thing is we need to insert some rows to our table.

    [sql]
    tran.executeSql(‘insert into Users (id, Name, MailID) values (1, "Sibi","sibikv4u@gmail.com")’);
    tran.executeSql(‘insert into Users (id, Name, MailID) values (2, "Aji","ajaybhasy@gmail.com")’);
    tran.executeSql(‘insert into Users (id, Name, MailID) values (3, "Ansu","ansary.ans21@gmail.com")’);
    [/sql]

    If you want to assign name, mailid, id values to insert query, you are welcomed to create those variables and assign to the query as shown below.

    [js]
    var name = "Sibi";
    var id = "1";
    var MailID = "sibikv4u@gmail.com";

    tran.executeSql(‘insert into Users (id, Name, MailID) values (?,?,?)’,[id,name,MailID]);
    [/js]

    So we have inserted some values too. Now we need to read the data we have inserted to our table right? To do that we can use we need to create a new transaction and another executeSql command.

    [js]
    tran.executeSql(‘SELECT * FROM Users’, [], function (tran, data) {
    });
    [/js]

    Here we will get the output in data. As you can see I have given a call back function along with the command. This can be used to loop through our data and shows the same in our page. So we can modify our reading transaction block as follows.

    [js]
    myDBInstance.transaction(function (tran) {
    var html = ‘<table><thead><th>Mail ID </th><th>ID</th><th>Name </th></thead><tbody>’;
    tran.executeSql(‘SELECT * FROM Users’, [], function (tran, data) {
    for (i = 0; i < data.rows.length; i++) {
    html += ‘<tr><td>’
    + ‘<a ‘ + ‘href="mailto:’ + data.rows[i].MailID + ‘">’ + data.rows[0].MailID + ‘</a>’ +
    ‘</td><td>’ + data.rows[i].id + ‘</td><td>’ + data.rows[i].Name + ‘</td></tr>’;
    };
    html += ‘</tbody></table>’;
    $(‘#myTab’).html(html);
    });
    });
    [/js]

    Before that,

  • Please don’t forget to include jQuery reference
  • Do not forget to create a div with id myTab
  • You can add a CSS for the table we are creating dynamically as follows.

    [css]
    <style>
    table,tr,td,th {
    border:1px solid #ccc;
    border-radius:5px;
    padding:10px;
    margin:10px;
    }

    </style>
    [/css]

    Complete code

    Complete code for the implementation is given below.

    [html]
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Introduction to Web SQL</title>
    <script src="Scripts/jquery-1.11.1.min.js"></script>
    <script type="text/javascript">
    var myDBInstance = openDatabase(‘dbsibeeshpassion’, ‘1.0’, ‘This is a client side database’, 3 * 1024 * 1024);
    //check whether the database is created or not.
    if (!myDBInstance) {
    alert(‘Oops, your database was not created’);
    }
    else {
    var version = myDBInstance.version;
    //var name = "Sibi";
    //var id = "1";
    //var MailID = "sibikv4u@gmail.com";
    myDBInstance.transaction(function (tran) {
    tran.executeSql(‘CREATE TABLE IF NOT EXISTS Users (id unique, Name, MailID)’);
    //tran.executeSql(‘insert into Users (id, Name, MailID) values (?,?,?)’, [id, name, MailID]);
    tran.executeSql(‘insert into Users (id, Name, MailID) values (1, "Sibi","sibikv4u@gmail.com")’);
    tran.executeSql(‘insert into Users (id, Name, MailID) values (2, "Aji","ajaybhasy@gmail.com")’);
    tran.executeSql(‘insert into Users (id, Name, MailID) values (3, "Ansu","ansary.ans21@gmail.com")’);
    });
    myDBInstance.transaction(function (tran) {
    var html = ‘<table><thead><th>Mail ID </th><th>ID</th><th>Name </th></thead><tbody>’;
    tran.executeSql(‘SELECT * FROM Users’, [], function (tran, data) {
    for (i = 0; i < data.rows.length; i++) {
    html += ‘<tr><td>’
    + ‘<a ‘ + ‘href="mailto:’ + data.rows[i].MailID + ‘">’ + data.rows[0].MailID + ‘</a>’ +
    ‘</td><td>’ + data.rows[i].id + ‘</td><td>’ + data.rows[i].Name + ‘</td></tr>’;
    };
    html += ‘</tbody></table>’;
    $(‘#myTab’).html(html);
    });
    });
    }

    </script>
    <style>
    table,tr,td,th {
    border:1px solid #ccc;
    border-radius:5px;
    padding:10px;
    margin:10px;
    }

    </style>
    </head>
    <body>
    <div id="myTab"></div>
    </body>
    </html>
    [/html]

    Output

    Web_SQL_Output

    Web_SQL_Output

    That is all. We did it. Have a happy coding.

    Conclusion

    Did I miss anything that you may think which is needed? Did you try Web SQL yet? Have you ever wanted to do this requirement? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.

    Your turn. What do you think?

    A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.

    Kindest Regards
    Sibeesh Venu

    TagsUsing Web SQLWeb SQL
    Previous Article

    Most Needed WordPress Plugins

    Next Article

    Sending data to another domain usign postMessage

    0
    Shares
    • 0
    • +
    • 0
    • 0
    • 0

    SibeeshVenu

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer.

    Related articles More from author

    • CodeProjectJQueryJson

      Sort a JSON Array Programmatically by a Property

      June 10, 2015
      By SibeeshVenu
    • Create Tooltip Without Any Plugins
      CodeProjectHTMLHTML5JQuery

      Create Tooltip Without Any Plugins

      September 29, 2015
      By SibeeshVenu
    • CodeProjectInterviewQ&A

      Interview Questions For Experienced and Beginner .NET Professionals

      June 16, 2015
      By SibeeshVenu
    • Thunderbird_output
      How toJavaScript

      Do you know JavaScript? Are you sure? – Part Two

      March 5, 2017
      By SibeeshVenu
    • CodeProjectJQuery

      Load Data on Scroll Using jQuery

      June 30, 2015
      By SibeeshVenu
    • AzureHow to

      How to host a website in Azure

      June 1, 2015
      By SibeeshVenu
    0

    My book

    Asp Net Core and Azure with Raspberry Pi Sibeesh Venu

    YouTube

    MICROSOFT MVP (2016-2022)

    profile for Sibeesh Venu - Microsoft MVP

    Recent Posts

    • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment
    • Build, Deploy, Configure CI &CD Your Static Website in 5 mins
    • Easily move data from one COSMOS DB to another
    • .NET 8 New and Efficient Way to Check IP is in Given IP Range
    • Async Client IP safelist for Dot NET
    • Post Messages to Microsoft Teams Using Python
    • Get Azure Blob Storage Blob Metadata Using PowerShell
    • Deploy .net 6 App to Azure from Azure DevOps using Pipelines
    • Integrate Azure App Insights in 1 Minute to .Net6 Application
    • Azure DevOps Service Connection with Multiple Azure Resource Group

    Tags

    Achievements (35) Angular (14) Angular 5 (7) Angular JS (15) article (10) Article Of The Day (13) Asp.Net (14) Azure (65) Azure DevOps (10) Azure Function (10) Azure IoT (7) C# (17) c-sharp corner (13) Career Advice (11) chart (11) CSharp (7) CSS (7) CSS3 (6) HighChart (10) How To (9) HTML5 (10) HTML5 Chart (11) Interview (6) IoT (11) Javascript (10) JQuery (82) jquery functions (9) JQWidgets (15) JQX Grid (17) Json (7) Microsoft (8) MVC (20) MVP (9) MXChip (7) News (18) Office 365 (7) Products (10) SQL (20) SQL Server (15) Visual Studio (10) Visual Studio 2017 (7) VS2017 (7) Web API (12) Windows 10 (7) Wordpress (9)
    • .NET
    • Achievements
    • ADO.NET
    • Android
    • Angular
    • Arduino
    • Article Of The Day
    • ASP.NET
    • Asp.Net Core
    • Automobile
    • Awards
    • Azure
    • Azure CDN
    • azure devops
    • Blockchain
    • Blog
    • Browser
    • C-Sharp Corner
    • C#
    • Career Advice
    • Code Snippets
    • CodeProject
    • Cognitive Services
    • Cosmos DB
    • CSS
    • CSS3
    • Data Factory
    • Database
    • Docker
    • Drawings
    • Drill Down Chart
    • English
    • Excel Programming
    • Exporting
    • Facebook
    • Fun
    • Gadgets
    • GitHub
    • GoPro
    • High Map
    • HighChart
    • How to
    • HTML
    • HTML5
    • Ignite UI
    • IIS
    • Interview
    • IoT
    • JavaScript
    • JQuery
    • jQuery UI
    • JQWidgets
    • JQX Grid
    • Json
    • Knockout JS
    • Linux
    • Machine Learning
    • Malayalam
    • Malayalam Poems
    • MDX Query
    • Microsoft
    • Microsoft ADOMD
    • Microsoft MVP
    • Microsoft Office
    • Microsoft Technologies
    • Microsoft Windows
    • Microsoft Windows Server
    • Mobile
    • MongoDB
    • Monthly Winners
    • MVC
    • MVC Grid
    • MySQL
    • News
    • Node JS
    • npm
    • Number Conversions
    • October 2015
    • Office 365
    • Office Development
    • One Plus
    • Outlook
    • Page
    • PHP
    • Poems
    • PowerShell
    • Products
    • Q&A
    • Raspberry PI
    • React
    • SEO
    • SharePoint
    • Skype
    • Social Media
    • Software
    • Spire.Doc
    • Spire.PDF
    • Spire.XLS
    • SQL
    • SQL Server
    • SSAS
    • SSMS
    • Storage In HTML5
    • Stories
    • Third Party Software Apps
    • Tips
    • Tools
    • Translator Text
    • Uncategorized
    • Unit Testing
    • UWP
    • VB.Net
    • Videos
    • Virtual Machine
    • Visual Studio
    • Visual Studio 2017
    • Wamp Server
    • Web API
    • Web Platform Installer
    • Webinars
    • WebMatrix
    • Windows 10
    • Windows 7
    • Windows 8.1
    • Wordpress
    • Writing

    ABOUT ME

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer. If you would like to know more about me, you can read my story here.

    Contact Me

    • info@sibeeshpassion.com

    Pages

    • About
    • Search
    • Privacy Policy
    • About
    • Search
    • Privacy Policy
    © Copyright Sibeesh Passion 2014-2025. All Rights Reserved.
    Go to mobile version