Read Existing SQLite Database File and Load Data

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!

1 Like

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

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. :slight_smile:

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!

1 Like

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

First of all, you need to import 3 plugins:

Then import the SQLite in your ts file:

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

export class HomePage {
    public storage: SQLite;
    public itemList: Array<Object>;
 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.

1 Like

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

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

Thank You. Will try it.

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

In what folder did you add the database?

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

I found one of the way to play with existing DB on Deploy Ionic 3 App with pre-populated SQLite DB from MySQL DB

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/ and put my db in www folder. That’s it.

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.

Are you running it using a real device?

Yes , i am testing it in real device , this is sample code
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.sqlite.create({
name: ‘AgroPod_db’,
location: ‘default’
}).then((db: SQLiteObject) => {
db.executeSql(‘SELECT * FROM mas_crop_generic’, {})
.then((res) => console.log(‘Executed SQL’, res))
.catch(e => console.log(e));
}).catch(e => console.log(e));
});
}

openDB() {
  	this.sqlite.create({
	  name: 'dbname.db',
	  location: 'default'
	}).then((db: SQLiteObject) => {
        /*creation here something like for example:*/
		db.executeSql("CREATE TABLE IF NOT EXISTS table_name_here(id INTEGER PRIMARY KEY AUTOINCREMENT, columnname1 TEXT, columnname2 TEXT)", {}).then(() => {
	    /*select sql here*/
	    });
	}).catch(e => /*log error here*/ );
  }

Thanks for the suggestion However , I need to use prepopulated DB to fetch the data as soon as the app loads so i may not need to create the tables.

Any lead would be appreciated…I am stuck with it from a week. :expressionless: