How to set the SQLite file in a specific location

I am using SQLite as suggested from this page http://ionicframework.com/docs/v2/native/sqlite/
but how do I know where is the *.db file location, since it set as default?
How to set a specific db file path to location?

1 Like

*but how do I know where is the .db file location, since it set as default?

The default location is the www folder, so you donā€™t need to do anything other than placing your db file on www folder and setting location='defalut' on SQLite. Image showing my DB inside www folder



In my case iā€™m using a pre-populated database , donā€™t know if itā€™s what you want.
The plugin iā€™m using is (GitHub - brodybits/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)). Install with ionic plugin add cordova-sqlite-ext --save

Basically this what i do:

First import SQLite

import { SQLite } from ā€˜ionic-nativeā€™;

Options to open database, name is the name of the database i already have, if you donā€™t have any database it should be the name to give to your DB.

let options = { name: ā€œMTS.dbā€, location: ā€˜defaultā€™, createFromLocation: 1 };

Create the connection

this.storage = new SQLite();

Open database and execute a simple query (my DB contains a table called stations)

this.storage.openDatabase(options).then((su) => {
  this.storage.executeSql('SELECT * FROM stations', {}).then((res) => {console.log(JSON.stringify(res))}, (err) => { 
    console.error('Unable to execute sql: ', err);
});
1 Like

What version of Ionic are you using? In Ionic 4 did not work because it recreates the folder www.

In that case it was Ionic 2. I havenā€™t used Ionic since Ionic 2. I have an example with Ionic 3 in Github ionic3-PreDB. But i havenā€™t updated it in a while.

Thanks man, I saw this example that you posted. But for me only version 4 same.

Did you ever figure this out? I am a little lost and would love some help.