# Issue creating a SQLite service

**URL:** <https://forum.ionicframework.com/t/issue-creating-a-sqlite-service/76890>\
**Category:** ionic-v3\
**Created:** [January 20, 2017, 6:46pm UTC](https://forum.ionicframework.com/t/issue-creating-a-sqlite-service/76890 "2017-01-20T18:46:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![enniob](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@enniob](https://forum.ionicframework.com/u/enniob)\
**Post date:** [January 20, 2017, 6:46pm UTC](https://forum.ionicframework.com/t/issue-creating-a-sqlite-service/76890/1 "2017-01-20T18:46:27Z")

</div>

I’m new to ionic2 and I’m having some issue creating a DB service for SQLite.

So I have the following service that will open a connection to my database

**dbservice.ts**

```
import { Injectable } from '@angular/core';
import { Platform } from 'ionic-angular';
import { SQLite } from 'ionic-native';

@Injectable()
   export class Dbservice {    
        db: SQLite;  

        constructor(public platform: Platform) {
            this.platform.ready().then(() => {
                this.db = new SQLite();
                this.db.openDatabase({name:'setup.db',location:'default'});
            });
        }        
}

```

On my app.comonents.ts I have the following code.

```
import { Component, ViewChild } from '@angular/core';
import { Nav, Platform } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';
import { Dbservice } from '../providers/dbservice';

@Component({
    templateUrl: 'app.html',
    providers: [Dbservice]
})

export class MyApp {
    @ViewChild(Nav) nav: Nav;
    rootPage: any = Home;
    pages: Array<{title: string, component: any}>;
    
    constructor(public platform: Platform, private dbService: Dbservice) {
        this.initializeApp();
        this.pages = [
            {title: 'Home', component: Home}
        ];
    }
    initializeApp() {
        this.platform.ready().then(() => {
            StatusBar.styleDefault();
            this.dbService.db.executeSql('CREATE TABLE IF NOT........ ',{}).then(() => {
                console.log('HERE');
             }, function(error){
                console.log(error);
             });
            
            Splashscreen.hide();
        });
    }
    
    openPage(page) {
        this.nav.setRoot(page.component);
    }
}

```

I get an error saying it cannot ready property of ‘executeSql’ of undefined.

Thanks,  
Ennio

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [January 20, 2017, 8:42pm UTC](https://forum.ionicframework.com/t/issue-creating-a-sqlite-service/76890/2 "2017-01-20T20:42:45Z")

</div>

If all you need is key/value storage, I’d suggest using ionic-storage. It’s much simpler.

If you really need SQL, then your problem is that you’re trying to use the database before it’s ready. The simplest way to solve this would be to expose a promise out of `Dbservice` that resolves once all the stuff you’re doing in the constructor is complete, and then wait on that promise in `initializeApp`, instead of just waiting on the platform. This is essentially how ionic-storage does it.

---

<div class="post-metadata">

**Author:** ![enniob](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@enniob](https://forum.ionicframework.com/u/enniob)\
**Post date:** [January 20, 2017, 9:56pm UTC](https://forum.ionicframework.com/t/issue-creating-a-sqlite-service/76890/3 "2017-01-20T21:56:53Z")

</div>

Thanks for the suggestion. That actually is perfect for what I need. 🙂
