4

I m using cordova to build hybrid android app and i m using this function which return database object its works fine everywhere in app

    function openDB() {
        var dbUser = null;
        var dBVersion = localStorage.getItem("db_version");
        if (dBVersion == null) {
            try {
                if (!window.openDatabase) {
                    console.log('db init failed');
                } else {
                    dbUser = window.openDatabase("dbname", "1.0.1", "local", 100000);

                }
            } catch(error) {
                console.log(e);
                if (e.name == "INVALID_STATE_ERR") {
                    console.log("Invalid database version.");
                } else {
                    console.log("Unknown error " + e + ".");
                }
            }
        } else {
            dbUser = window.openDatabase("dbname", dBVersion, "local", 100000);
        }
        console.log(dbUser);
        //initialize tables
        if (dbUser != null)
            createTables(dbUser);
        return dbUser;
    }

but when i use social plugin like facebook and foursquare and return to app then my app wont able to access the database and give error

Uncaught Error: SECURITY_ERR: DOM Exception 18 at file:///android_asset/www/js/DB.js:27 
dbUser = window.openDatabase("dbname", dBVersion, "local", 100000);

and the my app become blank as its wont able to access the database.

user3732629
  • 141
  • 15

1 Answers1

0

When do you execute your code?

If you check out this post you should only execute Database queries after your device is ready. So wait for the deviceready event triggered from cordova.

 document.addEventListener("deviceready", openDB, false);

Note, that this will only be triggered on the phone, not on the browser anymore.

I think for older cordova versions there was as well a limit of the database size. 100000 seems to be quite random, isn't it?

Hope this helps.

Community
  • 1
  • 1
Sebastian
  • 1,873
  • 4
  • 25
  • 57