Load pre populated SQLite database - impossible in Ionic 2


#1

Hi,

I was just wondering if there is a way to load an exising SQL database on Ionic 2 project load?
As I noticed, there is an option in SqlStorage:

if (win.sqlitePlugin) {
var location_1 = this._getBackupLocation(dbOptions.backupFlag);
this._db = win.sqlitePlugin.openDatabase(util_1.assign({
name: dbOptions.name,
location: location_1,
createFromLocation: dbOptions.existingDatabase ? 1 : 0
}, dbOptions));
}

but in any case the code wont run as ‘win.sqlitePlugin’ equals ‘undefined’.

Any ideas?


#2

Are you running in a browser or on a device? Cordova plugins don’t generally work in browser environments.


#3

Thanks rapropos for reply. The test that I have done was in browser env. - I will check it on device then.
However do you know the best way of integrating an existing DB on app run?


#4

I figure it’s dangerous to assume binary compatibility in all the different environments Ionic applications try to run in, so I just issue SQL to initialize databases.


#5

So how to fill in the DB with the initial data - any advice how it should look like?


#6

Since a week - no response. I jave tried several ways to integrate the data but with no result. Is there anyone who managed to load a pre filled database in Ionic 2? Thanks.


#7

Maybe you can do it using SQL…what do you think?


#8

Could you please give me more details regarding the code structure? Do you mean use an external db with Ionic 2 services?

Also question: why would you give up then with SQLite?


#9

We’re suggesting populating the database with ordinary SQL INSERT statements.


#10

This means that the solution is an external database.
My idea was to drop a ready database file avoiding any internet connexion.


#11

Nobody said anything about internet connections. SQLite is an SQL engine. You can issue CREATE TABLE and INSERT statements to it just as you can any other SQL engine.


Read Existing SQLite Database File and Load Data
#12

Ok but can you imagine the INSERT statement for 10 000 entries in the core of code - for me it’is dirty solution, or do you think maybe of json file as a source?