Access the SQLite db which is externally created

2 posts, 0 answers
  1. Arun Kumar
    Arun Kumar avatar
    1 posts
    Member since:
    Feb 2013

    Posted 05 May Link to this post

    Can anybody help me to connect the externally created SQLite db? I am new to telerik app builder. I have verified the sample todo app "Sample-To-Do-App-With-Sqlite" available in telerik site. They created a db and access the same. But I created a db from outside and I included the same in my project folder. I want to access it by cordova. 

     

    01.app.openDb = function () {
    02.    var dbName = "ARMS_SLATE.sqlite";
    03. 
    04.    //DB creation for Simulator
    05.    if (window.navigator.simulator === true) {
    06.        app.db = window.openDatabase(dbName, "1.0", "ARMS Slate Database", 1073741824);
    07.        console.log("Database Created!");
    08.    }
    09.    //DB creation for devices
    10.    else {
    11.        app.db = window.sqlitePlugin.openDatabase(dbName);
    12.        console.log("Database Accessed!");
    13.    }
    14.}

     

    In the above code I can create the ARMS_SLATE.sqlite db and access the same. But I want to access the DB which is placed under the data folder. Please refer in the attached image.

  2. Martin
    Admin
    Martin avatar
    67 posts

    Posted 09 May Link to this post

    Hi Arun,

    The window.sqlitePlugin.openDatabase and window.openDatabase will look for a file with the specified name in the default SQLite Database directory of the device. In case there is such a file they will open it. Otherwise it will create a new blank db and open it. Therefore you cannot open your own already populated .sqlite file with these commands as it is not located in the default SQLite Database directory of the device.

    If you want to use a pre-populated database I would suggest to do the following:

    1. After you execute the window.sqlitePlugin.openDatabase
      •  check if the database is empty:
        var db = window.sqlitePlugin.openDatabase(dbName);
          
        db.transaction(function(tx) {
            tx.executeSql("Select * from YourTable", [], successCallback, errorCallback);
        });

         
      • if the database is empty, populate it with your data:
        var db = window.sqlitePlugin.openDatabase(dbName);
          
        db.transaction(function(tx) {
            tx.executeSql("INSERT INTO TABLE_NAME (column1, column2, ...columnN)] VALUES (value1, value2, ...valueN)", [], successCallback, errorCallback);
        });


    2. You can try and use an external plugin like cordova-plugin-dbcopy that can copy your existing .sqlite database to the default SQLite Database directory of the device and then open it with window.sqlitePlugin.openDatabase.

     Let me know if this has worked for you.



    Regards,
    Martin
    Telerik
     

    Visit the Telerik Verified Plugins Marketplace and get the custom Cordova plugin you need, already tweaked to work seamlessly with AppBuilder.

     
Back to Top