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

SQLite

Improve this doc

Access SQLite databases on the device.

Repo: https://github.com/litehelpers/Cordova-sqlite-storage

Installation

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

Supported platforms

Usage

import { SQLite, SQLiteObject } from '@ionic-native/sqlite';

constructor(private sqlite: SQLite) { }

...

this.sqlite.create({
  name: 'data.db',
  location: 'default'
})
  .then((db: SQLiteObject) => {


    db.executeSql('create table danceMoves(name VARCHAR(32))', [])
      .then(() => console.log('Executed SQL'))
      .catch(e => console.log(e));


  })
  .catch(e => console.log(e));

Instance Members

create(config)

Open or create a SQLite database file.

See the plugin docs for an explanation of all options: https://github.com/litehelpers/Cordova-sqlite-storage#opening-a-database

Param Type Details
config SQLiteDatabaseConfig

database configuration

Returns: Promise

echoTest()

Verify that both the Javascript and native part of this plugin are installed in your application

Returns: Promise<any>

selfTest()

Automatically verify basic database access operations including opening a database

Returns: Promise<any>

deleteDatabase(config)

Deletes a database

Param Type Details
config SQLiteDatabaseConfig

database configuration

Returns: Promise<any>

SQLiteObject

Instance Members

databaseFeatures()

openDBs()

addTransaction()

transaction(fn)

Param Type Details
fn Function

Returns: Promise<any>

readTransaction(fn)

Param Type Details
fn Function

Returns: Promise<any>

startNextTransaction()

open()

Returns: Promise<any>

close()

Returns: Promise<any>

executeSql()

Execute SQL on the opened database. Note, you must call create first, and ensure it resolved and successfully opened the database.

sqlBatch(sqlStatements)

Param Type Details
sqlStatements Array<string | string[] | any>

Returns: Promise<any>

abortallPendingTransactions()

SQLiteDatabaseConfig

Param Type Details
name string

Name of the database. Example: 'my.db'

location string

Location of the database. Example: 'default'

(optional)
iosDatabaseLocation string

iOS Database Location. Example: 'Library'

(optional)
createFromLocation number

support opening pre-filled databases with https://github.com/litehelpers/cordova-sqlite-ext

(optional)
key string

support encrypted databases with https://github.com/litehelpers/Cordova-sqlcipher-adapter

(optional)

SQLiteTransaction

Param Type Details
start () => void
addStatement DbTransaction[

API

Native

General