SQLite Porter cordova plugin with Ionic 2 (RC1)

Hi, is there a way to use the SQLite Porter cordova plugin with Ionic 2 RC1 to import json into a database?

The plugin is this:

If not, other ways to import a json into the database?

Thanks for any help!

Yes you can use it. Since this plugin is still not included in Ionic Native, you will have to do a few things manually here.

Just follow the installation instructions on their repo, and then add the following line to the top of the page where you are planning to use the plugin:

declare var cordova: any

Alternatively, you can add that line in declarations.d.ts (which is included in new ionic 2 apps).

After you do that, you can now call the functions of the plugins as documented in their repo:

cordova.plugins.sqlitePorter.importSqlToDb(....);

I also recommend that you wrap your calls with something like:

this.platform.ready().then(() => {
  if (cordova && cordova.plugins && cordova.plugins.sqlitePorter) {
    // call plugin API here
  }
});

Thank you very much for your help. It seems working :slight_smile:

I have only to understand why I get an

Error: Uncaught (in promise): undefined

by polyfills.js:3 when I call the cordova.plugins.sqlitePorter.importSqlToDb(...)… this is not very clear… but the json is correctly imported into the db.

Well the importSqlToDb doesn’t use promises, so it’s probably something else.

Can you share the code you’re using, maybe I can spot an error

Probably it’s something related to the SQLite service that I implemented. I use sqliteporter plugin with sqlite-storage-ext plugin and the native file plugin. With the file plugin I read a file .json that has to be imported in the sqlite database.

So in the SQLite service I used:

...
@Injectable()
export class DatabaseProvider {

  private storage: SQLite;
  private isOpen: boolean;

  constructor(private platform: Platform) {
    platform.ready().then(() => {
      this.openDatabase();
    });
  }
  
  public openDatabase() {
    if (!this.isOpen) {
      this.storage = new SQLite();
      this.storage.openDatabase({name: "data.db", location: "default", createFromLocation: 1}).then(() => {
        this.isOpen = true;
      });
    }
  }

  public getDb() {
    return this.storage;
  }
...

I’m not sure if I’ve done in the correct way… I used the getDb() method for the parameter db in cordova.plugins.sqlitePorter.importSqlToDb(…); in this way:

this.platform.ready().then(() => {
  
  ...
  
  let db = this.database.getDb();
  
  ...
  
  File.readAsText(path, fileName).then((data) => {
    json = data;

    var successFn = function(count){
      Toast.show('Data imported', '3000', 'bottom').subscribe(toast => {});
    };
    
    var errorFn = function(error){
      console.log(error.message);
    };
    
    var progressFn = function(current, total){
        console.log('Imported '+current+'/'+total+' statements');
    };
            
    if (cordova && cordova.plugins && cordova.plugins.sqlitePorter) {
      cordova.plugins.sqlitePorter.importJsonToDb(db, json, {
        successFn: successFn,
        errorFn: errorFn,
        progressFn: progressFn
      });
    }
    
  }, (error) => {
    console.error(error);
  });
  
}); 

Maybe the problem is related to the way I passed the db parameter?

Thank you very much for your help!

Well the only thing I see is that you’re not catching any errors that might be thrown by openDatabase … but if everything is working fine that might not be it. Try adding a second function inside the .then() or add .catch() to see if it catches the error you see in your console.

I tried with the function in .then but it doesn’t catch the error. I will continue to investigate…

Anyway, thank you very much for your support!

my code doesn’t go pass this line
‘if(cordova && cordova.plugins && cordova.plugins.sqlporter)’
and without than line too I get plugin_not_installed for sqliteporter though it’s installed.
any reason?
I’m on ionic 3 trying to use the sqliteporter.

my plugin list is attached.
ionic_cordova_plugin_ls

Please assist me