# Read Existing SQLite Database File and Load Data

**URL:** <https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167>\
**Category:** Ionic Native\
**Created:** [June 13, 2017, 5:10pm UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167 "2017-06-13T17:10:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![junjunhernandez](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@junjunhernandez](https://forum.ionicframework.com/u/junjunhernandez)\
**Post date:** [June 13, 2017, 5:10pm UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/1 "2017-06-13T17:10:38Z")

</div>

Hi,

I am new to Ionic and I have been working on SQLite but I cannot make this work for a week now, and its quite frustrating 😕 .  
I have tried some plugins such as litehelpers/Cordova-sqlite-storage, litehelpers/cordova-sqlite-ext, and some others but it doesnt seem to work for me. Some plugins return “Cannot find sqlitePlugin” error.  
I also tried openDatabase() { this.sqlite.create({name: ‘data.db’, location: ‘default’}).then((db: SQLiteObject) but it does not read my existing database. BTW, I am using a real Android device to test.

What I want to achieve is, just load the data from the database that I previously created using DB Browser for SQLite. I have tried putting the sqlite file into my www but its still not working. I got errors like cannot open database. The database that I am talking to about already has tables and data stored so I will just be reading them and showing data to the user. Is it possible to achieve this?

I have tried some solutions on some posts like [Load pre populated SQLite database - impossible in Ionic 2](https://forum.ionicframework.com/t/load-pre-populated-sqlite-database-impossible-in-ionic-2/54601/11) and [How to make SQLite in ionic-native use cordova-sqlite-ext?](https://forum.ionicframework.com/t/how-to-make-sqlite-in-ionic-native-use-cordova-sqlite-ext/68131/15)

Its difficult to imagine creating hundreds or thousands of INSERT statements that is why I just want to load the database with existing tables and data.

Thanks! Any help will be gladly appreciated. 🙂 .

Anyway, below is my ionic info:

global packages:

```
@ionic/cli-utils : 1.4.0
Cordova CLI : 7.0.1
Ionic CLI : 3.4.0

```

local packages:

```
@ionic/app-scripts : 1.3.7
@ionic/cli-plugin-cordova : 1.4.0
@ionic/cli-plugin-ionic-angular : 1.3.1
Cordova Platforms : android 6.2.3 browser 4.1.0 ios 4.4.0
Ionic Framework : ionic-angular 3.3.0

```

System:

```
Node : v7.6.0
OS : Windows 10
Xcode : not installed
ios-deploy : not installed
ios-sim : not installed
npm : 4.1.2
```

---

<div class="post-metadata">

**Author:** ![junjunhernandez](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@junjunhernandez](https://forum.ionicframework.com/u/junjunhernandez)\
**Post date:** [June 14, 2017, 10:40am UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/2 "2017-06-14T10:40:38Z")

</div>

[https://www.thepolyglotdeveloper.com/2015/01/deploy-ionic-framework-app-pre-filled-sqlite-db/](https://www.thepolyglotdeveloper.com/2015/01/deploy-ionic-framework-app-pre-filled-sqlite-db/) doesnt seem to help me because I think that Ionic framework used here is an old version.

---

<div class="post-metadata">

**Author:** ![junjunhernandez](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@junjunhernandez](https://forum.ionicframework.com/u/junjunhernandez)\
**Post date:** [June 14, 2017, 6:16pm UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/3 "2017-06-14T18:16:14Z")

</div>

Hi. I hope someone notice my inquiry. By the way, the app that I am creating is an offline language tutorial.

---

<div class="post-metadata">

**Author:** ![Nejthe](https://avatars.discourse-cdn.com/v4/letter/n/838e76/32.png) [@Nejthe](https://forum.ionicframework.com/u/Nejthe)\
**Post date:** [August 6, 2017, 7:24am UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/4 "2017-08-06T07:24:19Z")

</div>

Hello, do you still need help?

---

<div class="post-metadata">

**Author:** ![junjunhernandez](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@junjunhernandez](https://forum.ionicframework.com/u/junjunhernandez)\
**Post date:** [August 7, 2017, 12:29pm UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/5 "2017-08-07T12:29:42Z")

</div>

I don’t need help on this anymore. I just became helpless on this. What I did is put all the data in a json file and encode them into something. Thanks!

---

<div class="post-metadata">

**Author:** ![Nejthe](https://avatars.discourse-cdn.com/v4/letter/n/838e76/32.png) [@Nejthe](https://forum.ionicframework.com/u/Nejthe)\
**Post date:** [August 7, 2017, 1:31pm UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/6 "2017-08-07T13:31:34Z")

</div>

Do you think it’s better than having a database?

---

<div class="post-metadata">

**Author:** ![junjunhernandez](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@junjunhernandez](https://forum.ionicframework.com/u/junjunhernandez)\
**Post date:** [August 7, 2017, 1:44pm UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/7 "2017-08-07T13:44:29Z")

</div>

Of course it is not. Its just that I really do not know how to solve my problem and I became helpless. I even searched about it on search engines for one week without success. But anyway, thanks for your willingness to help. 🙂

---

<div class="post-metadata">

**Author:** ![Nejthe](https://avatars.discourse-cdn.com/v4/letter/n/838e76/32.png) [@Nejthe](https://forum.ionicframework.com/u/Nejthe)\
**Post date:** [August 8, 2017, 8:59pm UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/8 "2017-08-08T20:59:11Z")

</div>

Well i had the same problem, i actually spent weeks trying to get it to work and finally succeeded. if you or anyone else are still interested on how I was able to do it let me know!

---

<div class="post-metadata">

**Author:** ![hemant07rulz](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/hemant07rulz/32/93865_2.png) [@hemant07rulz](https://forum.ionicframework.com/u/hemant07rulz)\
**Post date:** [August 9, 2017, 2:19am UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/9 "2017-08-09T02:19:19Z")

</div>

Please help me on this. I am struggling on it for a week !

---

<div class="post-metadata">

**Author:** ![Nejthe](https://avatars.discourse-cdn.com/v4/letter/n/838e76/32.png) [@Nejthe](https://forum.ionicframework.com/u/Nejthe)\
**Post date:** [August 9, 2017, 8:29am UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/10 "2017-08-09T08:29:50Z")

</div>

First of all, you need to import 3 plugins:

> **[an-rahulpandey/cordova-plugin-dbcopy](https://github.com/an-rahulpandey/cordova-plugin-dbcopy)**
>
> cordova-plugin-dbcopy - Copy SQLite Database from www folder to default app database location

> **[litehelpers/Cordova-sqlite-storage](https://github.com/litehelpers/Cordova-sqlite-storage)**
>
> Cordova-sqlite-storage - A Cordova/PhoneGap plugin to open and use sqlite databases on Android, iOS and Windows with HTML5/Web SQL API

> **[litehelpers/cordova-sqlite-ext](https://github.com/litehelpers/cordova-sqlite-ext)**
>
> cordova-sqlite-ext - A Cordova/PhoneGap plugin to open and use sqlite databases on Android/iOS/macOS/Windows with REGEXP (Android/macOS/iOS) and pre-populated databases (Android/iOS/macOS/Windows)

Then import the SQLite in your ts file:

```auto
import { Component } from '@angular/core';
import { SQLite } from "ionic-native";
import { Platform, NavController, PopoverController } from 'ionic-angular';

```

```auto

export class HomePage {
    public storage: SQLite;
    public itemList: Array<Object>;

```

```auto
 constructor(public popoverCtrl: PopoverController, public navCtrl: NavController, public platform: Platform) {

   this.itemList = [];

        this.platform.ready().then(() => {
            this.storage = new SQLite();
            this.storage.openDatabase({ name: "data.db", location: 'default', createFromLocation: 1 }).then((success) => {
                this.storage.executeSql("SELECT * FROM art", {}).then((data) => {
                    let rows = data.rows;
                    for (let i = 0; i < rows.length; i++) {
                        this.itemList.push({
                            id: rows.item(i).id
                        });
                    }
                }, (error) => {
                    console.info("Unable to execute sql " + JSON.stringify(error));
                })
         }, (err) => {
                console.info("Error opening database: " + err);
            });
        });
    }

```

Don’t forget to add the file data.db in www/ directory.

I hope I didn’t miss anything. Let me know if it works.

---

<div class="post-metadata">

**Author:** ![hemant07rulz](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/hemant07rulz/32/93865_2.png) [@hemant07rulz](https://forum.ionicframework.com/u/hemant07rulz)\
**Post date:** [August 9, 2017, 3:40pm UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/11 "2017-08-09T15:40:12Z")

</div>

Do I have to call the `Copy()` method to copy the Database ? Or just including the `cordova-plugin-dbcopy` Plugin is enough ?

---

<div class="post-metadata">

**Author:** ![Nejthe](https://avatars.discourse-cdn.com/v4/letter/n/838e76/32.png) [@Nejthe](https://forum.ionicframework.com/u/Nejthe)\
**Post date:** [August 9, 2017, 4:16pm UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/12 "2017-08-09T16:16:15Z")

</div>

Just include the Plugin. That’s how it worked with me!

---

<div class="post-metadata">

**Author:** ![hemant07rulz](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/hemant07rulz/32/93865_2.png) [@hemant07rulz](https://forum.ionicframework.com/u/hemant07rulz)\
**Post date:** [August 9, 2017, 4:22pm UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/13 "2017-08-09T16:22:40Z")

</div>

Thank You. Will try it.

---

<div class="post-metadata">

**Author:** ![hemant07rulz](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/hemant07rulz/32/93865_2.png) [@hemant07rulz](https://forum.ionicframework.com/u/hemant07rulz)\
**Post date:** [August 9, 2017, 11:50pm UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/14 "2017-08-09T23:50:07Z")

</div>

It does not work. Throws `error: No Such table`. Whereas I have the correct table in the Database I created using Sqlite browser.

---

<div class="post-metadata">

**Author:** ![Nejthe](https://avatars.discourse-cdn.com/v4/letter/n/838e76/32.png) [@Nejthe](https://forum.ionicframework.com/u/Nejthe)\
**Post date:** [August 29, 2017, 4:01pm UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/15 "2017-08-29T16:01:27Z")

</div>

In what folder did you add the database?

---

<div class="post-metadata">

**Author:** ![freezacoder](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/freezacoder/32/113004_2.png) [@freezacoder](https://forum.ionicframework.com/u/freezacoder)\
**Post date:** [September 5, 2017, 2:46pm UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/16 "2017-09-05T14:46:09Z")

</div>

Do you have any idea why should I include those 3 if you used just one?

---

<div class="post-metadata">

**Author:** ![musmanzpak](https://avatars.discourse-cdn.com/v4/letter/m/3ec8ea/32.png) [@musmanzpak](https://forum.ionicframework.com/u/musmanzpak)\
**Post date:** [October 20, 2017, 12:06pm UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/17 "2017-10-20T12:06:22Z")

</div>

I found one of the way to play with existing DB on [Deploy Ionic 3 App with pre-populated SQLite DB from MySQL DB](http://blog.academytimes.com/ionic-3-framework/deploy-ionic-3-app-pre-populated-sqlite-db-mysql-db/)

---

<div class="post-metadata">

**Author:** ![junjunhernandez](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@junjunhernandez](https://forum.ionicframework.com/u/junjunhernandez)\
**Post date:** [October 20, 2017, 1:02pm UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/18 "2017-10-20T13:02:59Z")

</div>

Though I previously said that I put my data into json, I was able to solve it in another project. I just followed [https://ionicframework.com/docs/native/sqlite/](https://ionicframework.com/docs/native/sqlite/) and put my db in www folder. That’s it.

---

<div class="post-metadata">

**Author:** ![priaahuja](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@priaahuja](https://forum.ionicframework.com/u/priaahuja)\
**Post date:** [November 28, 2017, 10:27am UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/19 "2017-11-28T10:27:23Z")

</div>

Hi…I tried to do the same i put my db file in www folder and assets folder as well but it is still showing “no such table exists” error.Please let me know if i missed something.

---

<div class="post-metadata">

**Author:** ![junjunhernandez](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@junjunhernandez](https://forum.ionicframework.com/u/junjunhernandez)\
**Post date:** [November 28, 2017, 10:29am UTC](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/20 "2017-11-28T10:29:56Z")

</div>

Are you running it using a real device?

[Next page](https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167.md?page=2)
