Read Existing SQLite Database File and Load Data

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