Software Archive
Read-only legacy content
17061 Discussions

Reading prepopulated sqlite database in app.

puneet_d_
Beginner
684 Views

I am working with intel XDK and there I have a prepopulated .db file which i need to read inside my code. As we do in native app where we put db files in assets directory and then access those database by copying them in app's database directory. I am new to xdk.

0 Kudos
11 Replies
PaulF_IntelCorp
Employee
684 Views

Try using the SQLite plugin and following his instructions on how to add your DB to the project.

0 Kudos
puneet_d_
Beginner
684 Views

Paul F. (Intel) wrote:

Try using the SQLite plugin and following his instructions on how to add your DB to the project.

I tried that way..adding sqlite plugin, but didnt succeed. Any sample code would be helpful!

 

0 Kudos
PaulF_IntelCorp
Employee
684 Views

What you asking is beyond the scope of this forum. I recommend you search StackOverflow for help with adding a DB to your Cordova or PhoneGap app. The XDK creates Cordova apps, anything you find there that addresses your issue can be used with the XDK.

0 Kudos
Pamela_H_Intel
Moderator
684 Views

Puneet - many of Qnimate's tutorials are a little old but they are helpful in describing processes., The Local Database Storage tutorial (http://qnimate.com/local-database-storage-using-intel-xdk/), is a great one to read through it you will understand the basic process of storing data locally. The plugin he uses seems to be gone, but once you decide which plugin to use you can look at the function calls for that plugin. Our plugin manager has changed significantly as well - you will find it in Build Settings in the PROJECT page.

0 Kudos
puneet_d_
Beginner
684 Views

I tried every way to add and access pre-populated sqlite database through cordova sqlite storage plugin by lite helpers, also i tried ext version of this plugin as it is mentioned somewhere that it works but i didn't succeed doing this. please provide support with a sample project so that i can understand and implement that. requirement  is simple, I have an .db format sqlite databse file which i created with Sqlite Browser, Now I need to read that file in my app. 

0 Kudos
Miguel_Ángel_M_
Beginner
684 Views

Hi punnet,

You must copy your database in www directory and you need a plugin, i.e https://github.com/an-rahulpandey/cordova-plugin-dbcopy, to copy de database from this directory to default directory in device.

 // And the code must be something like this

window.plugins.sqlDB.copy("prueba.db", 1,  function () {

   var db = window.sqlitePlugin.openDatabase({name: "prueba.db", location: 'default'});

 }

The problem is this code only works when you build and install app, but not in debug mode.

Hope this help you,

Miguel Ángel

0 Kudos
Hamilton_Tenório_da_
Valued Contributor I
684 Views

After a long research and thinking, I do backup and restore database using a server.

My problem to solve was that my app has a database created and populated by the user, but without backup. 

Now I have 2 buttons: backup and restore.

The function for each one is:

BACKUP

$(document).on("click", "#botBackup", function()
    {
        myApp.showPreloader("copiando");
        var options = new FileUploadOptions();
        options.fileKey = wNomeDB;
        options.fileName = wNomeDB + "_" + device.uuid;
        options.mimeType = "multipart/form-data";
        db.close(function() {console.log("db fechado");}, function() {console.log("não fechou db");});
        var ft = new FileTransfer();
        ft.upload(cordova.file.applicationStorageDirectory + "databases/" + wNomeDB, encodeURI("http://server/suporte/upload.php"), winCopyDB, failCopyDB, options);
});

 

RESTORE

$(document).on("click", "#botRestore", function()
    {
        myApp.showPreloader("restaurando");
        db.close(function() {console.log("db fechado");}, function() {console.log("não fechou db");});
        window.sqlitePlugin.deleteDatabase({name: wNomeDB, location: 2}, function() {console.log("db apagado");}, function() {console.log("db NÃO apagado");});
        var fileTransfer = new FileTransfer();
        var uri = encodeURI("http://server/suporte/download.php?id=" + device.uuid);
        var fileURL = cordova.file.applicationStorageDirectory + "databases/" + wNomeDB;
        console.log(fileURL);

        fileTransfer.download(
            uri,
            fileURL,
            function(entry) {
                //prepara o banco de dados
                db = window.sqlitePlugin.openDatabase({
                    name: wNomeDB,
                    location: 2
                });
                myApp.hidePreloader();
            },
            function(error) {
                console.log("download error source " + error.source);
                console.log("download error target " + error.target);
                console.log("upload error code" + error.code);
                myApp.hidePreloader();
            },
            false,
            {
                headers: {
                    "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
                }
            }
        );
    });

 

For each use, I have a PHP program in the server to handle the action.

Some remarks:

wNomeDB = name of my database

device.uuid = used to distinguish each database user in the server

 

If you have the database on the server, you can just access it to create copies inside the app.

0 Kudos
Miguel_Ángel_M_
Beginner
684 Views

Using dbcopy plugin you can use a prepopulated database from www directory, like I show in previous comment, but only when build my application and install it in a device. However when I try to debug it in the device (using USB connection) the application can't access database because dbcopy can't copy it from www directory to default location. The error is that the database doen't exists in www directory.

Debuging the pluging code I can see that really the www directory doesn't contain the database. Intel XDK documentation talk about debug modules and I think the problem is that the application contents (including database file) can't be accessed in debug mode. Then, how can access my application files to copy db file to default location? Is there any way to create a custom debug module to include my database in it?

Thaks in advance,

Miguel Ángel

 

0 Kudos
PaulF_IntelCorp
Employee
684 Views

Miguel -- the debug modules are not able to work for all situations. In those cases, you need to debug on a built app. See this link for help on debugging a built app > https://software.intel.com/en-us/xdk/docs/intel-xdk-debug-and-test-overview#RemoteChromeDevTools <

0 Kudos
Miguel_Ángel_M_
Beginner
684 Views

Thanks Paul,

The problem with this solution is that you must build the application and install it to test every change in the application. Is there no way to debug an application with a prepopulated sqlite database without having to build and install it?

0 Kudos
PaulF_IntelCorp
Employee
684 Views

There are clever ways around avoiding rebuilding the app. You can use the JavaScript console to redefine functions (rewrite the function in your source file and copy and paste it into the remote CDT JavaScript console). This might require restructuring some of your code, the code you are debugging and also requires that you understand the difference between early and late binding, but it works quite well. Once of the beauties of dealing with a scripted dynamic language.

Watch the video on this page > https://software.intel.com/en-us/xdk/docs/using-the-test-tab < especially around the 20:00 minute point for an example of what I mean.

0 Kudos
Reply