This is a migrated thread and some comments may be shown as answers.

Access the SQLite db which is externally created

1 Answer 234 Views
AppBuilder extension for Visual Studio
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Arun Kumar
Top achievements
Rank 1
Arun Kumar asked on 05 May 2016, 06:56 AM

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.

1 Answer, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 09 May 2016, 03:09 PM

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.

 
Tags
AppBuilder extension for Visual Studio
Asked by
Arun Kumar
Top achievements
Rank 1
Answers by
Martin
Telerik team
Share this question
or