Software Archive
Read-only legacy content
17060 Discussions

WebSQL x SQLitePlugin: how to define only once the use of database

Hamilton_Tenório_da_
Valued Contributor I
1,407 Views

I have a APP with WebSQL. It works OK on Android build. Now I´m testing the same APP with Cordova build. There is on problem: the database of old version is lost when a new Cordova version is installed. 

For this moment, according with the comments, I assumed that isn´t have a solution. Then, I am trying to prevent it for the next situation like that. The solution is the use of SQLitePlugin (I hope).

But, there is a situation that I need help (maybe it is only a javascript problem that I don´t know...). Today I have the OPENDATABASE at the initial part of the javascript (global) and the use of variable in any function. Something like this

var wTamanhoDB = 1 * 1024 * 1024; //10 Mb
var wDetalheDB = "APP ConfirmAki Pro";
var wNomeDB = "dbConfirmakiPro";
var wVersaoDB = "";
var db = window.openDatabase(wNomeDB, wVersaoDB, wDetalheDB, wTamanhoDB);

function x {
db.transaction(populateDB, errorCB, successCB);
}

function x {
...
db.transaction(populateDB, errorCB, successCB);
...
}

function y {
...
db.transaction(function(tx) {...}
...
}

function y {
...
db.transaction(function(tx) {...}
...
}

 

But, to use the plugin, I need to wait "deviceready", so, my "var db" is inside a function. It will be local. Then, all code must be changed, like this:

var wTamanhoDB = 1 * 1024 * 1024; //10 Mb
var wDetalheDB = "APP ConfirmAki Pro";
var wNomeDB = "dbConfirmakiPro";
var wVersaoDB = "";

function x {
var db = sqlitePlugin.openDatabase(wNomeDB, wVersaoDB, wDetalheDB, wTamanhoDB);
db.transaction(populateDB, errorCB, successCB);
}

function x {
...
var db = sqlitePlugin.openDatabase(wNomeDB, wVersaoDB, wDetalheDB, wTamanhoDB);
db.transaction(populateDB, errorCB, successCB);
...
}

function y {
...
var db = sqlitePlugin.openDatabase(wNomeDB, wVersaoDB, wDetalheDB, wTamanhoDB);
db.transaction(function(tx) {...}
...
}

function y {
...
var db = sqlitePlugin.openDatabase(wNomeDB, wVersaoDB, wDetalheDB, wTamanhoDB);
db.transaction(function(tx) {...}
...
}

 

Is it right? In every function I will open the database. Is it correct? There is another solution? Thank you.

0 Kudos
1 Solution
Barry_Johnson
New Contributor I
1,407 Views

Without knowing how the rest of your app is laid out, I would assume you could:

a) keep "db" as a global. Just define it is "var db" - it will be undefined.

b) open the database inside your device ready event and assign it to the db variable.

c) continue to use your "db.transaction" calls as-is. You could do an "undefined' check on db if the functions might be invoked before your device ready.

If this was too brief, let me know. You could do a lot more with this, and I personally would, but I tried to describe the minimal level of change in relation to the code you posted.

View solution in original post

0 Kudos
7 Replies
Barry_Johnson
New Contributor I
1,408 Views

Without knowing how the rest of your app is laid out, I would assume you could:

a) keep "db" as a global. Just define it is "var db" - it will be undefined.

b) open the database inside your device ready event and assign it to the db variable.

c) continue to use your "db.transaction" calls as-is. You could do an "undefined' check on db if the functions might be invoked before your device ready.

If this was too brief, let me know. You could do a lot more with this, and I personally would, but I tried to describe the minimal level of change in relation to the code you posted.

0 Kudos
PaulF_IntelCorp
Employee
1,407 Views

I recommend you use this plugin https://github.com/brodysoft/Cordova-SQLitePlugin since it uses a native implementation so that means it is part of the native app data, so your database should stick around for updates to your app.

0 Kudos
Hamilton_Tenório_da_
Valued Contributor I
1,407 Views

Great! It works! And this version (Cordova Crosswalk) is faster than. All user operations is smarter. Only the size of the APP is huge... I will send to some users to test before publish it on Google Play. Thank you!

0 Kudos
Barry_Johnson
New Contributor I
1,407 Views

Glad to hear it is working. Thanks Paul for the useful clarification on the recommended plugin. Crosswalk is awesome but it does definitely bulk up the app. I think it's well worth the trade-off myself, hopefully your users agree.

0 Kudos
PaulF_IntelCorp
Employee
1,407 Views

We're working on ways to reduce that size issue, but I do believe that the additional size (due to the included webview) far outweighs the alternative of making every Android native webview work! Please put both versions of the Crosswalk image into the store, so their is an x86 and an ARM image available for all devices. If you do not then you'll get some people that download an ARM image onto an x86 device and things will not work properly for them. The versionCode has been designed to make this process transparent.

0 Kudos
Barry_Johnson
New Contributor I
1,407 Views

" I do believe that the additional size (due to the included webview) far outweighs the alternative of making every Android native webview work!"

Couldn't agree more. Having a single target for Android makes for much, much more pleasant development.


I realized rereading my last messages the "hopefully your users agree" may have appeared it was directed at Intel. I mean that I hope Hamilton's users aren't bothered by the file size.

Now if you could just "crosswalk" iOS. LOL

0 Kudos
Hamilton_Tenório_da_
Valued Contributor I
1,407 Views

Thank you both about the comments. I need to resolve only one issue before publish this APP as Crosswalk on Google Play (https://software.intel.com/en-us/forums/topic/542976). I think that performance will be well received by users.

0 Kudos
Reply