Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use WebSql to store data in Phongap App

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 1.27k
    Comment on it

    Hello
    Today I am going to discuss about the use of WebSql in Phonegap.

    In Phonegap, if you want to store data locally on device, Web SQL can be used. Most of the sql queries can be performed in Web sql using method openDatabase that takes 4 arguments. first is database name, version number, text description and size of database. It also has method transaction and executeSql, transaction method is used for performing queries action and with the help of this you can call executeSql for insert, update and delete queries.

    Permissions that are required for WebSql:

    • Android: Add plugin Storage in app/res/xml/config.xml
    • <plugin name="Storage" value="org.apache.cordova.Storage"/>
      

    • In other platform like iOS, Bada, webOS, windows phone there is no need to add any permission. These devices use their own built-in support for storage.
    Here is the full example of using Websql:

    document.addEventListener("deviceready", onDeviceReady, false);
    
    // Populate the database 
    //
    function populateDB(tx) {
         tx.executeSql('DROP TABLE IF EXISTS DEMO');
         tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
         tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
         tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
    }
    // Select row of table
    function queryDB(tx) {
        tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
    }
     // Query the success callback
    //
    function querySuccess(tx, results) {
       var len = results.rows.length;
       console.log("DEMO table: " + len + " rows found.");
       for (var i=0; i<len; i++) {
            alert("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " + results.rows.item(i).data);
       }
    }
    // Transaction error callback
    //
    function errorCB(error) {
       alert(error in processing "+error.code);
    }
    
    // Transaction success callback
    //
    function successCB() {
        var db = window.openDatabase("Database", "1.0", WebSql demo, 200000);
         db.transaction(queryDB, errorCB);
    }
    
    function onDeviceReady() {
       var db = window.openDatabase("Database", "1.0", WebSql demo, 200000);
       db.transaction(populateDB, errorCB, successCB);
    } 
    

    You can also use where clause for selecting data from table as:

    function queryDB(tx) {
         tx.executeSql('SELECT Col1,Col2 FROM DEMO WHERE Id=?, [], querySuccess, errorCB);
    }
    

    Hope this blog will help you to store your data in local db, this can be also used for making App working in Offline mode.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: