Ionic-framework: Ionic 2.0.0 RC.0: Storage, SqlStorage and Native SQLite

Created on 29 Sep 2016  路  5Comments  路  Source: ionic-team/ionic-framework

Hi,

With the changes in the storage in the release candidate, can I ask this question?

I used to use this code, without any need for the Ionic Native SQLite plugin:

import {Storage, SqlStorage} from 'ionic-angular';
constructor() {
    this.storage1 = new Storage(SqlStorage, {name:'userData'});
    this.storage2 = new Storage(SqlStorage, {name:'taskData'});
  }

This doesn't work any more:

src/app/app.module.ts(4,19): error TS2305: Module '"/home/ubuntu/workspace/pathfinder/node_modules/@ionic/storage/dist/es5/index"' has no exported member 'SqlStorage'.

So what is the right syntax to use so that the storage will use SQLite if available and fall back if not?

Does it now involve the Ionic Native SQLite plugin?

Thanks

Tom

Most helpful comment

Duplicate #8269

All 5 comments

Duplicate #8269

@ionic-storage use Localforage is Storage now is provider see https://github.com/driftyco/ionic-conference-app/blob/master/src/providers/user-data.ts

Hello! Thanks for using Ionic. I am going to be closing this issue as a duplicate of https://github.com/driftyco/ionic/issues/8269

in app.module.ts

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

providers: [
StatusBar,
SplashScreen,
SQLite,
{ provide: ErrorHandler, useClass: IonicErrorHandler }
]


in database provider

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
//import { Storage } from '@ionic/storage';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
import { Platform } from 'ionic-angular';

/*
Generated class for the Database provider.

See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class Database {

DB_NAME: string = 'ssddb';

constructor(public http: Http, public platform: Platform, public sqlite: SQLite) {
    this.platform.ready().then(() => {
        this.configureDatabase();
    });
}

configureDatabase() {
    this.query('CREATE TABLE IF NOT EXISTS EMP (key text primary key, value text)')
        .catch(err => {
            console.error('Unable to create initial storage tables', err.tx, err.err);
        });
}

query(query: string, params: any[] = []): Promise<any> {
    return new Promise((resolve, reject) => {
        try {
            this.sqlite.create({
                name: this.DB_NAME,
                location: 'default'
            })
                .then((db: SQLiteObject) => {
                    db.transaction((tx: any) => {
                        tx.executeSql(query, params,
                            (tx: any, res: any) => resolve({ tx: tx, res: res }),
                            (tx: any, err: any) => reject({ tx: tx, err: err }));
                    })
                })
                .catch(e => console.log(e));
        } catch (err) {
            reject({ err: err });
        }
    });
}

get(key: string): Promise<any> {
    return this.query('select key, value from EMP where key = ? limit 1', [key])
        .then(data => {
            if (data.res.rows.length > 0) {
                return data.res.rows.item(0).value;
            }
        });
}

set(key: string, value: string): Promise<any> {
    return this.query('insert into EMP(key, value) values (?, ?)', [key, value]);
}

}

in page.ts
this.database.set("333","ss");

this.database.get("333").then(data => {
console.log(data);
let toast = this.toastCtrl.create({
message: data,
duration: 10000,
position: 'bottom'
});
toast.present();
});

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

giammaleoni picture giammaleoni  路  3Comments

MrBokeh picture MrBokeh  路  3Comments

fdnhkj picture fdnhkj  路  3Comments

brandyscarney picture brandyscarney  路  3Comments

brandyscarney picture brandyscarney  路  3Comments