# SqlLite plugin to work on web aswell like on device?

**URL:** <https://forum.ionicframework.com/t/sqllite-plugin-to-work-on-web-aswell-like-on-device/16438>\
**Category:** uncategorized\
**Created:** [January 27, 2015, 10:26am UTC](https://forum.ionicframework.com/t/sqllite-plugin-to-work-on-web-aswell-like-on-device/16438 "2015-01-27T10:26:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Endiss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/endiss/32/7786_2.png) [@Endiss](https://forum.ionicframework.com/u/Endiss)\
**Post date:** [January 27, 2015, 10:26am UTC](https://forum.ionicframework.com/t/sqllite-plugin-to-work-on-web-aswell-like-on-device/16438/1 "2015-01-27T10:26:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dalefrancis88](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dalefrancis88/32/76226_2.png) [@dalefrancis88](https://forum.ionicframework.com/u/dalefrancis88)\
**Post date:** [January 27, 2015, 3:30pm UTC](https://forum.ionicframework.com/t/sqllite-plugin-to-work-on-web-aswell-like-on-device/16438/2 "2015-01-27T15:30:40Z")

</div>

Hey,

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

Dale

---

<div class="post-metadata">

**Author:** ![nicraboy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nicraboy/32/6917_2.png) [@nicraboy](https://forum.ionicframework.com/u/nicraboy)\
**Post date:** [January 27, 2015, 4:05pm UTC](https://forum.ionicframework.com/t/sqllite-plugin-to-work-on-web-aswell-like-on-device/16438/3 "2015-01-27T16:05:17Z")

</div>

Hi @Endiss,

You can do something like this in your code:

```auto
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.

Regards,

---

<div class="post-metadata">

**Author:** ![Endiss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/endiss/32/7786_2.png) [@Endiss](https://forum.ionicframework.com/u/Endiss)\
**Post date:** [January 27, 2015, 4:39pm UTC](https://forum.ionicframework.com/t/sqllite-plugin-to-work-on-web-aswell-like-on-device/16438/4 "2015-01-27T16:39:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nicraboy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nicraboy/32/6917_2.png) [@nicraboy](https://forum.ionicframework.com/u/nicraboy)\
**Post date:** [January 27, 2015, 5:01pm UTC](https://forum.ionicframework.com/t/sqllite-plugin-to-work-on-web-aswell-like-on-device/16438/5 "2015-01-27T17:01:16Z")

</div>

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.

> **[Use SQLite Instead of Local Storage In Ionic Framework](https://www.thepolyglotdeveloper.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/)**
>
> Implement SQLite data storage in your Ionic Framework Android and iOS application instead of the commonly used local storage.

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.

Regards,

---

<div class="post-metadata">

**Author:** ![Endiss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/endiss/32/7786_2.png) [@Endiss](https://forum.ionicframework.com/u/Endiss)\
**Post date:** [January 27, 2015, 7:10pm UTC](https://forum.ionicframework.com/t/sqllite-plugin-to-work-on-web-aswell-like-on-device/16438/6 "2015-01-27T19:10:10Z")

</div>

@nicraboy  
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  
;/

---

<div class="post-metadata">

**Author:** ![nicraboy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nicraboy/32/6917_2.png) [@nicraboy](https://forum.ionicframework.com/u/nicraboy)\
**Post date:** [January 27, 2015, 7:17pm UTC](https://forum.ionicframework.com/t/sqllite-plugin-to-work-on-web-aswell-like-on-device/16438/7 "2015-01-27T19:17:17Z")

</div>

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?

Regards,

---

<div class="post-metadata">

**Author:** ![Endiss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/endiss/32/7786_2.png) [@Endiss](https://forum.ionicframework.com/u/Endiss)\
**Post date:** [January 27, 2015, 8:44pm UTC](https://forum.ionicframework.com/t/sqllite-plugin-to-work-on-web-aswell-like-on-device/16438/8 "2015-01-27T20:44:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Endiss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/endiss/32/7786_2.png) [@Endiss](https://forum.ionicframework.com/u/Endiss)\
**Post date:** [January 28, 2015, 7:37am UTC](https://forum.ionicframework.com/t/sqllite-plugin-to-work-on-web-aswell-like-on-device/16438/9 "2015-01-28T07:37:27Z")

</div>

Okay i have tested thanks @nicraboy

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

Cheers m8 🙂

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