Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre-populated database in ionic 2

I have some static data and I have created a SQLite database file using DB Browser for SQLite for same. Now want use that SQLite database file in my Ionic 2 application.

I looked into https://forum.ionicframework.com/t/read-existing-sqlite-database-file-and-load-data/94167/10 post but didn't get a solution.

Can anyone please help me!!!

Ionic info:

@ionic/cli-utils  : 1.9.2
ionic (Ionic CLI) : 3.9.2

Global packages:

Cordova CLI : 7.0.1

Local packages:

@ionic/app-scripts : 2.0.2
Cordova Platforms  : none
Ionic Framework    : ionic-angular 3.5.3
like image 923
Gurvinder Rajpal Avatar asked Nov 07 '22 17:11

Gurvinder Rajpal


1 Answers

You can achieve this using

cordova-sqlite-ext

for install,

cordova plugin add cordova-sqlite-ext --save

A Cordova/PhoneGap plugin to open and use sqlite databases on Android/iOS/Windows with REGEXP (Android/iOS) and pre-populated databases (Android/iOS/Windows)

var db = null;
document.addEventListener('deviceready', function() {
  db = window.sqlitePlugin.openDatabase({name: 'demo.db', location: 'default'});
});

IMPORTANT: Like with the other Cordova plugins your application must wait for the deviceready event. This is especially tricky in Angular/ngCordova/Ionic controller/factory/service callbacks which may be triggered before the deviceready event is fired.

Here's detailed example for this.

Ionic Framework App With Pre-Filled SQLite DB

like image 54
codeMonkey Avatar answered Nov 15 '22 13:11

codeMonkey