Where to put SQLite databases on Ionic 2 RC0?

Ok, I think I’ve got some results, I don’t know if everything is ok, but at least I can can connect to a prepopulated database and select some data from a table.

  1. I installed the cordova-sqlite-ext plugin

  2. In app.component.ts I imported “import { SQLite } from 'ionic-native';

  3. In the platform.ready() I inserted:

    let db = new SQLite();
    db.openDatabase({
    name: “data.db”,
    location: “default”,
    createFromLocation: 1
    }).then(() => {
    db.executeSql(“SELECT * from config”, []).then((data) => {
    console.log("Data received: ", data);
    }, (error) => {
    console.error(“Unable to execute sql”, error);
    })
    }, (error) => {
    console.error(“Unable to open database”, error);
    });

  4. I create a file called copy.config.js at the same path of package.json and I inserted:

    module.exports = {
    include: [
    {
    src: ‘src/assets/’,
    dest: ‘www/assets/’
    },
    {
    src: ‘src/index.html’,
    dest: ‘www/index.html’
    },
    {
    src: ‘src/data.db’,
    dest: ‘www/data.db’
    },
    {
    src: ‘src/service-worker.js’,
    dest: ‘www/service-worker.js’
    },
    {
    src: ‘node_modules/ionic-angular/polyfills/polyfills.js’,
    dest: ‘www/build/polyfills.js’
    },
    {
    src: ‘node_modules/ionicons/dist/fonts/’,
    dest: ‘www/assets/fonts/’
    },
    ]
    };

  5. In the file package.json I inserted:

    “config”: {
    “ionic_copy”: “./copy.config.js”
    },

before the last line "description": "SqlProject: An Ionic project".

That’s all for the moment. From the console I can see that it can open the database and receive the data from the config table inside the data.db database that I placed in the src folder.

Of course, all suggestions, improvements and test results are appreciated! :slight_smile:

2 Likes