SqlLite plugin to work on web aswell like on device?

Hello so i’m trying to do that when the nGCordova sqllite plugin is called to work on webbrowser like on device how i can do this?

when im uisng on browser getting error TypeError: Cannot read property ‘openDatabase’ of undefined


Have you got the ngCordova script tag before your cordova script tag?
Does it already work on device?


Hi @Endiss,

You can do something like this in your code:

if(window.cordova) {
    db = $cordovaSQLite.openDB({name: "my.db"});
} else {
    db = window.openDatabase("my.db", '1', 'my database', 5 * 1024 * 1024);

If the plugin exists then we are on device and ngCordova should be used. Otherwise, use WebSQL for the browser.


1 Like

@nicraboy so if im using same app on device and web it will work with this statement? because cordova is there i think?

Depends on if the rest of your code is correct.

If you’ve followed my guide, you should be able to put that if-statement in the .run() method of the code.

As long as you’ve got a valid db variable everything else in ngCordova should work since the only method of ngCordova that depends on the plugin is openDB.

I’ll see about integrating this into the library in the near future.


Ye but when i run db = $cordovaSQLite.openDB({ name: “my.db” }); its giving me error of sqlite plugin from ngcordowa that
TypeError: Cannot read property ‘openDatabase’ of undefined

Yes you need to do a check to see if you’re on mobile or web. Mind showing your source code on GitHub so we can identify the problem?


Cannot shar it atm:) maybe you can tell me how to chceck if im on web ?

Okay i have tested thanks @nicraboy

now my code looks like hope it will work as intend to !:wink:

Cheers m8 :slight_smile:

if(window.cordova) {
    db = $cordovaSQLite.openDB({name: app.dbName , bgType: 1});
} else {
    db = window.openDatabase(app.dbName , '1', 'ES Database', 5 * 1024 * 1024);