# Ionic 2 SQLite not opening database

**URL:** <https://forum.ionicframework.com/t/ionic-2-sqlite-not-opening-database/65213>\
**Category:** ionic-v3\
**Created:** [September 30, 2016, 3:05pm UTC](https://forum.ionicframework.com/t/ionic-2-sqlite-not-opening-database/65213 "2016-09-30T15:05:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![richardmarais](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/richardmarais/32/101456_2.png) [@richardmarais](https://forum.ionicframework.com/u/richardmarais)\
**Post date:** [September 30, 2016, 3:05pm UTC](https://forum.ionicframework.com/t/ionic-2-sqlite-not-opening-database/65213/1 "2016-09-30T15:05:28Z")

</div>

Hi All,

I am using Ionic 2, and following [this tutorial](https://www.thepolyglotdeveloper.com/2015/12/use-sqlite-in-ionic-2-instead-of-local-storage/).

My problem is I cannot seem to open the database. I have it deployed to an Android Emulator called KOPLAYER.

**app.ts**

```
  initializeApp() {
    this.platform.ready().then(() => {
      StatusBar.styleDefault();
      if (window.cordova) {
        this.createDatabase();
      }
    });
  }

```

and

```
  private createDatabase(): void {
    let db: SQLite = new SQLite();
    db.openDatabase({
      name: "data.db",
      location: "default"
    }).then(() => {
      db.executeSql("CREATE TABLE IF NOT EXISTS chats (_id TEXT PRIMARY KEY, memberIds TEXT, title TEXT, subTitle TEXT, picture TEXT, lastMessageId TEXT, lastMessageCreatedAt DATE)", {}).then((chatData) => {
        console.log("chats TABLE CREATED: ", chatData);
        db.executeSql("CREATE TABLE IF NOT EXISTS messages (_id TEXT PRIMARY KEY, chatId TEXT, senderId TEXT, ownership TEXT, content TEXT, createdAt DATE, changeDate BOOLEAN, readByReceiver BOOLEAN)", {}).then((messageData) => {
          console.log("messages TABLE CREATED: ", messageData);
        }, (error) => {
          console.error("Unable to execute messages sql", error);
        });

      }, (error) => {
        console.error("Unable to execute chats sql", error);
      });
    }, (error) => {
      console.error("Unable to open database", error);
    });
  }

```

**storageService.ts**

`public database: SQLite;`

```
constructor() {
    if (window.cordova) {
        this.openDatabase();
    }
}

```

and

```
private openDatabase(): void {
    console.log('openDatabase');
    this.database.openDatabase({ name: "data.db", location: "default" }).then(() => {
        this.refreshChats();
        this.refreshMessages();
    }, (error) => {
        console.log("ERROR: ", error);
    });
}

```

`openDatabase` gets outputted to the console, but the `this.refreshChats();` does not get called. The database creation in `app.ts` looks correct according to the console logs.

> ```
> OPEN database: data.db SQLitePlugin.js:175
> OPEN database: data.db - OK SQLitePlugin.js:179
> chats TABLE CREATED: Object app.bundle.js:215
> messages TABLE CREATED: Object app.bundle.js:217
> 
> ```

then

> `openDatabase`

I use `chrome://inspect/#devices` to view the console output. Are there any other tools I can use to view the database running on the emulator?

Any help appreciated.

---

<div class="post-metadata">

**Author:** ![richardmarais](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/richardmarais/32/101456_2.png) [@richardmarais](https://forum.ionicframework.com/u/richardmarais)\
**Post date:** [October 1, 2016, 7:09am UTC](https://forum.ionicframework.com/t/ionic-2-sqlite-not-opening-database/65213/2 "2016-10-01T07:09:02Z")

</div>

My mistake, I forgot the `this.database = new SQLite();`

```
private openDatabase(): void {
    this.database = new SQLite();
    this.database.openDatabase({ name: "data.db", location: "default" }).then(() => {
        this.refreshChats();
        this.refreshMessages();
    }, (error) => {
        console.log("ERROR: ", error);
    });
}
```

---

<div class="post-metadata">

**Author:** ![jeethu](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@jeethu](https://forum.ionicframework.com/u/jeethu)\
**Post date:** [December 19, 2018, 7:22am UTC](https://forum.ionicframework.com/t/ionic-2-sqlite-not-opening-database/65213/3 "2018-12-19T07:22:16Z")

</div>

Hi All,  
Iam using ionic1, and my problem iam running my app in xcode simulator, and while login iam getting \*\*close: db name was not open: ![18%20PM](https://global.discourse-cdn.com/ionicframework/original/3X/b/5/b5a48cff1590757a6128f5855fe793932c35af69.png) \*\*
