Need help upgrading to Ionic Framework 4.0? Get assistance with our Enterprise Migration Services EXPLORE NOW

SQLite Porter

Improve this doc

This Cordova/Phonegap plugin can be used to import/export to/from a SQLite database using either SQL or JSON.

Repo: https://github.com/dpa99c/cordova-sqlite-porter

Installation

  1. Install the Cordova and Ionic Native plugins:
    $ ionic cordova plugin add uk.co.workingedge.cordova.plugin.sqliteporter
    $ npm install --save @ionic-native/sqlite-porter@4
    
  2. Add this plugin to your app's module

Supported platforms

Usage

import { SQLitePorter } from '@ionic-native/sqlite-porter';


constructor(private sqlitePorter: SQLitePorter) { }

...

let db = window.openDatabase('Test', '1.0', 'TestDB', 1 * 1024);
// or we can use SQLite plugin
// we will assume that we injected SQLite into this component as sqlite
this.sqlite.create({
  name: 'data.db',
  location: 'default'
})
  .then((db: any) => {
    let dbInstance = db._objectInstance;
    // we can pass db._objectInstance as the database option in all SQLitePorter methods
  });


let sql = 'CREATE TABLE Artist ([Id] PRIMARY KEY, [Title]);' +
           'INSERT INTO Artist(Id,Title) VALUES ("1","Fred");';

this.sqlitePorter.importSqlToDb(db, sql)
  .then(() => console.log('Imported'))
  .catch(e => console.error(e));

Instance Members

importSqlToDb(db, sql)

Executes a set of SQL statements against the defined database. Can be used to import data defined in the SQL statements into the database, and may additionally include commands to create the table structure.

Param Type Details
db Object

Database object

sql string

SQL statements to execute against the database

Returns: Promise<any>

exportDbToSql(db)

Exports a SQLite DB as a set of SQL statements.

Param Type Details
db Object

Database object

Returns: Promise<any>

importJsonToDb(db, json)

Converts table structure and/or row data contained within a JSON structure into SQL statements that can be executed against a SQLite database. Can be used to import data into the database and/or create the table structure.

Param Type Details
db Object

Database object

json Object|string

JSON structure containing row data and/or table structure as either a JSON object or string

Returns: Promise<any>

exportDbToJson(db)

Exports a SQLite DB as a JSON structure

Param Type Details
db Object

Database object

Returns: Promise<any>

wipeDb(db)

Wipes all data from a database by dropping all existing tables

Param Type Details
db Object

Database object

Returns: Promise<any>

API

Native

General