Saturday 5 December 2020

How to Retrieving data from SQLite database?

 In our previous section, we learned how we can create a table in the SQLite database and how we can insert the data into the database table. In this section, we will learn about how we can fetch the data from the database and how it will be visible on the user interface.

We will use the select statement to fetch data from the table and use the unordered list as a list view where our data will be shown:

These are the following steps used to fetch data from the SQLite database:

1) Create index2.html file

We will create a new file index2.html with the same code as present in the index.html. We will make changes later in index2.html file not in index.html.

Retrieving data from SQLite database
Retrieving data from SQLite database

2) Creating a list view

Now, we will first create a list view using <ul></ul> tags. This list view will show all the result on the user interface. We will give an id to this list view and code in between the <body></body> tags as:

  1. <body>  
  2.         <div data-role="page">  
  3.         <div data-role="header"><h1> SQLite database example </h1></div>  
  4.         <div data-role="main" class="ui-content">  
  5.         <label for="name"> Name </label>  
  6.         <input type="text" id="name" />  
  7.         <label for="email"> Email </label>  
  8.         <input type="text" id="email" />  
  9.         <button id="btnSave" type="submit"> Save </button>  
  10.         <ul id="members" data-role="listview">  
  11.         </ul>  
  12.         <script type="text/javascript" src="cordova.js"></script>  
  13.         </div><!-- main-->  
  14.         </div><!-- page -->  
  15. </body>  


Retrieving data from SQLite database

3) Create and execute the select statement

Now, we will go to our saveRecord function and do the modification here. We will add a select statement using the transaction.executeSql function. This function will also have four parameters, i.e., select statement array getSuccess callback function, and getError callback function. We will add the select statement in the following way:

  1. transaction.executeSql("SELECT * FROM employeesList", [], getSuccess, getError);  

4) Create getSuccess function

Now, we will create the getSuccess function in between the <script></script> tags. The getSuccess function will have two parameters, i.e., transaction and result. In this function, we will create a row object and create a loop for showing the name and email of each record. We will create a variable for both the name and the email, and then we will add this name and email to our list view in the following way:

  1.         function getSuccess(tx, result)  
  2.         {  
  3.             var rows = result.rows;  
  4.             for(var x=0; x< rows.length; x++)  
  5. {  
  6.                 var name = result.rows[x].Name; <!--Fetching name -->  
  7.                 var email = result.rows[x].Email; <!?Fetching Email -->  
  8.                 var out = "<li>" + name + "<br/>" + email + "</li>"; <!?Store data in list-->  
  9.                 document.getElementById('members').innerHTML += out; <!?Adding list in the list view -->  
  10.             }   
  11.               
  12.             $('#members').listview('refresh'); <!--Refreshing list view -->  
  13.         }  

5) Create getError function

The getError function will be used to show the error on the console if it is raised. The getError function will be created in the following way:

  1. function getError(e)  
  2. {  
  3.     console.log(e);  
  4. }  

Complete Code:

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="utf-8">  
  5.         <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">  
  6.         <title>SQLLite DB App</title>  
  7.         <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />  
  8.         <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>  
  9.         <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>  
  10.         <script>  
  11.         var db;  
  12.               
  13.         window.onload=function()// it is a function call.  
  14.         {  
  15.             document.getElementById('btnSave').addEventListener('click', saveData);  
  16.             db = window.openDatabase("employees",  "1.0", "LearnToProgram", 200000);  
  17.         }  
  18.           
  19.         function saveData(e)  
  20.         {  
  21.             db.transaction(saveRecord, onSuccess, onError);  
  22.         }  
  23.               
  24.         function saveRecord(transaction)  
  25.         {  
  26.             var namedocument.getElementById('name').value;  
  27.             var email = document.getElementById('email').value;  
  28.             transaction.executeSql('CREATE TABLE IF NOT EXISTS employeesList (id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL, Email TEXT NOT NULL) ');  
  29.               
  30.             var sql"INSERT INTO employeesList (Name,Email) VALUES ('" + name +"', '" + email + "')";  
  31.             console.log(sql);  
  32.             transaction.executeSql(sql);  
  33.             transaction.executeSql("SELECT * FROM employeesList", [], getSuccess, getError);  
  34.         }  
  35.         function getSuccess(tx, result)  
  36.         {  
  37.             var rows = result.rows;  
  38.             for(var x=0; x< rows.length; x++){  
  39.                 var name = result.rows[x].Name;  
  40.                 var email = result.rows[x].Email;  
  41.                 var out = "<li>" + name + "<br/>" + email + "</li>";  
  42.                 document.getElementById('members').innerHTML += out;  
  43.             }  
  44.               
  45.             $('#members').listview('refresh');  
  46.         }  
  47.         function getError(e)  
  48.         {  
  49.             console.log(e);  
  50.         }  
  51.               
  52.         function onSuccess()  
  53.         {  
  54.             console.log("Record Saved");  
  55.         }  
  56.               
  57.         function onError(error)  
  58.         {  
  59.             console.log(error);  
  60.         }  
  61.         </script>  
  62.     </head>  
  63.     <body>  
  64.         <div data-role="page">  
  65.         <div data-role="header"><h1> SQLite Database Example</h1></div>  
  66.         <div data-role="main" class="ui-content">  
  67.         <label for="name">Name</label>  
  68.         <input type="text" id="name" />  
  69.         <label for="email">Email</label>  
  70.         <input type="text" id="email" />  
  71.         <button id="btnSave" type="submit">Save</button>  
  72.         <ul id="members" data-role="listview">  
  73.         </ul>  
  74.         <script type="text/javascript" src="cordova.js"></script>  
  75.         </div><!-- main-->  
  76.         </div><!-- page -->  
  77.     </body>  
  78. </html>  

Output:

Retrieving data from SQLite database

No comments:

Post a Comment