Issue creating a SQLite service

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


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

   export class Dbservice {    
        db: SQLite;  

        constructor(public platform: Platform) {
            this.platform.ready().then(() => {
                this.db = new SQLite();

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';

    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.pages = [
            {title: 'Home', component: Home}
    initializeApp() {
        this.platform.ready().then(() => {
            this.dbService.db.executeSql('CREATE TABLE IF NOT........ ',{}).then(() => {
             }, function(error){
    openPage(page) {

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


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.

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