How to use Sqlite with ionic 2 rc.0?

前端 未结 3 2202
猫巷女王i
猫巷女王i 2020-12-03 16:14

I would like to know how to use Sqlite with Ionic 2 rc.o release.I am finding it difficult as there are no examples for the latest version release and i am stuck.Nothing on

3条回答
  •  时光说笑
    2020-12-03 16:49

    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 {
            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 {
            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 {
            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();
            });
    

提交回复
热议问题