# Open db before load init view

**URL:** <https://forum.ionicframework.com/t/open-db-before-load-init-view/29213>\
**Category:** uncategorized\
**Created:** [July 26, 2015, 12:43am UTC](https://forum.ionicframework.com/t/open-db-before-load-init-view/29213 "2015-07-26T00:43:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![agucabral](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/agucabral/32/13740_2.png) [@agucabral](https://forum.ionicframework.com/u/agucabral)\
**Post date:** [July 26, 2015, 12:43am UTC](https://forum.ionicframework.com/t/open-db-before-load-init-view/29213/1 "2015-07-26T00:43:18Z")

</div>

My start view must show a list of elements from the sqlite db, have managed to select and insert from the db and the way i open the db is like in this example [https://blog.nraboy.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/](https://blog.nraboy.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/).

```
rrApp.run(function($ionicPlatform, $state, $cordovaSQLite) {
$ionicPlatform.ready(function() {
	
	if (window.cordova && window.cordova.plugins.Keyboard) {
		cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
	}
	if (window.StatusBar) {
		StatusBar.styleDefault();
	}
	
	db = $cordovaSQLite.openDB("my.db");
	$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS EVENTS (ID integer primary key, NAME text)");
   	$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS TASKS (ID integer primary key, EVENT_ID integer, NAME text)");   	 
   	   	
});

```

the problem i have is when i execute $cordovaSQLite .execute(db,“SELECT \* FROM TABLE”) it always return this error:  
TypeError: Cannot call method ‘transaction’ of null

how can i succesfully open the db before the controller try to retrieve something from it?

---

<div class="post-metadata">

**Author:** ![Gajotres](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/gajotres/32/98564_2.png) [@Gajotres](https://forum.ionicframework.com/u/Gajotres)\
**Post date:** [July 26, 2015, 1:49pm UTC](https://forum.ionicframework.com/t/open-db-before-load-init-view/29213/2 "2015-07-26T13:49:17Z")

</div>

That’s because ionicPlatform ready sometimes don’t work as intended.

Instead you should use default Cordova:

```
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is ready
function onDeviceReady() {
  var db = window.sqlitePlugin.openDatabase({name: "my.db"});
  // ...
}
```

---

<div class="post-metadata">

**Author:** ![bengtler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bengtler/32/98565_2.png) [@bengtler](https://forum.ionicframework.com/u/bengtler)\
**Post date:** [July 26, 2015, 3:15pm UTC](https://forum.ionicframework.com/t/open-db-before-load-init-view/29213/3 "2015-07-26T15:15:02Z")

</div>

If you want to be sure that the db is opened before any view is shown or something like this  
–\> add a root state to your app and use uiRouters resolve property where you open the dbconnection

> **[angular-ui/ui-router](https://github.com/angular-ui/ui-router/wiki/Nested-States-%26-Nested-Views#inherited-resolved-dependencies)**
>
> ui-router - The de-facto solution to flexible routing with nested views in AngularJS

---

<div class="post-metadata">

**Author:** ![agucabral](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/agucabral/32/13740_2.png) [@agucabral](https://forum.ionicframework.com/u/agucabral)\
**Post date:** [August 6, 2015, 8:41pm UTC](https://forum.ionicframework.com/t/open-db-before-load-init-view/29213/4 "2015-08-06T20:41:32Z")

</div>

I apologise for the delay, after i tried using both of your aproaches and getting no results (maybe because i didnt do it right), I solved this by using :

db = $cordovaSQLite.openDB(“my.db”);  
$cordovaSQLite.execute(db, “CREATE TABLE IF NOT EXISTS EVENTS (ID integer primary key, NAME text)”);  
$cordovaSQLite.execute(db, “CREATE TABLE IF NOT EXISTS TASKS (ID integer primary key, EVENT\_ID integer, NAME text)”);  
**$rootScope.$broadcast(‘dbInitialized’);**

and adding this in the controller

**var scope = $rootScope.$new();**  
**scope.$on(‘dbInitialized’, function() {**  
**$scope.loadRoutines();**  
**})**

I’m not sure its the best way but Its working, do you think this is a bad way to do iy?  
thank you both!

---

<div class="post-metadata">

**Author:** ![leopiazzoli](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/leopiazzoli/32/96712_2.png) [@leopiazzoli](https://forum.ionicframework.com/u/leopiazzoli)\
**Post date:** [February 12, 2016, 6:02pm UTC](https://forum.ionicframework.com/t/open-db-before-load-init-view/29213/5 "2016-02-12T18:02:38Z")

</div>

I’m solved this problem as well:

* * *

**app.js:**

```
$ionicPlatform.ready(function () { 
        db = $cordovaSQLite.openDB("my.db");
        alert("HERE 1");
    });

```

* * *

**controller:**

```
$ionicPlatform.ready(function () {
    alert("HERE 2");
    // SELECT 
});

```

* * *

Thus alert 1 comes before alert 2

---

<div class="post-metadata">

**Author:** ![Zalakain](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/zalakain/32/108877_2.png) [@Zalakain](https://forum.ionicframework.com/u/Zalakain)\
**Post date:** [April 26, 2017, 4:16pm UTC](https://forum.ionicframework.com/t/open-db-before-load-init-view/29213/6 "2017-04-26T16:16:10Z")

</div>

May not be the best, but at least your works! Thanks mate! 🙂
