Sunday, 17 April 2016

How to work with SQLite in Phonegap?

What is SQLite?
      Serverless, zero-configuration, very small, light weight, self-contained(no external dependency). It is written in ANSI-C. Available on UNIX(Linux, Mac OS-X, Android, iOS) and Windows.

SQLite in Phonegap:
      First, need to install SQLite plugin in phonegap working project(Assuming you already created phonegap project). Go to that phonegap working project in cmd(windows) and type below command to install SQLite plugin.

>> phonegap plugin add cordova-sqlite-storage

Now we are going to implement CRUD operations with SQLite using jQuery:

Sample Syntax for all operations:

     var db = window.openDatabase({name:"TestDb.db"});   - To create database and connection

     db.transaction(function(transaction){                               - For database transactions
          var query = "SQL query";
          transaction.executeSql(query, [parameters],                - For query execution
          function(tx, result){
                 //success
          },
         function(error){
        });                                                               
     });

Table creation in database:

     db.transaction(function(transaction){                           
          var query = "CREATE TABLE IF NOT EXISTS phonegapsolutions (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, usermail TEXT)";
          transaction.executeSql(query, [],                               
          function(tx, result){
                 alert("Table created successfully");
          },
         function(error){
                 alert("Error in table creation");
        });                                                               
     });

Insertion:

     var username = "edukondalu";
     var usermail = "edukondaluthaviti@gmail.com";
    db.transaction(function(transaction){                           
          var query = "INSERT into phonegapsolutions (username, usermail) VALUES (?, ?)";
          transaction.executeSql(query, [username, usermail],                               
          function(tx, result){
                 alert("Inserted");
          },
         function(error){
                 alert("Error in insertion");
        });                                                               
     });

Get all inserted data:

    db.transaction(function(transaction){                           
          var query = "SELECT * from phonegapsolutoins";
          transaction.executeSql(query, [],                               
          function(tx, result){
                 for(var i=0;i<result.rows.length;i++){
                     var item = result.rows.item(i);
                     var html = "<li>" + item["username"] + " " + item["usermail"]  + "</li>";
                }
               $("#id").html(html);
          },
         function(error){
                 alert("Error in Getting all data");
        });                                                               
     });

Updation:

     var id = $("#id").text();
     var username = $("#username").val();
     var usermail = $("#usermail").val();
    db.transaction(function(transaction){                           
          var query = "UPDATE phonegapsolutions SET username = ?, usermail = ?where id = ?";
          transaction.executeSql(query, [username, usermail, id],                               
          function(tx, result){
                 alert("Updated");
          },
         function(error){
                 alert("Error in updation");
        });                                                               
     });

Deletion:

    db.transaction(function(transaction){                           
          var query = "DELETE from phonegapsolutions where id = ?";
          transaction.executeSql(query, [id],                               
          function(tx, result){
                 alert("Deleted");
          },
         function(error){
                 alert("Error in deletion");
        });                                                               
     });

DROP:

    db.transaction(function(transaction){                           
          var query = "DELETE TABLE IF EXISTS phonegapsolutions";
          transaction.executeSql(query, [],                               
          function(tx, result){
                 alert("Table Deleted");
          },
         function(error){
                 alert("Error in table deletion");
        });                                                               
     });

Please let me know any help from phonegap side.

1 comment: