2016-11-16 9 views
2

私はテストタブイオン2アプリを使用するsqliteプラグインを使用します。 app.componentsでイオン2のsqliteをアンドロイドで実行:プロパティexecuteSqlを読み取ることができませんundefined

import { SQLite, Device } from 'ionic-native'; 
import { Injectable } from '@angular/core'; 
import { Http } from '@angular/http'; 
import 'rxjs/add/operator/map'; 

/* 
    Generated class for the SqliteHelper provider. 

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

    public db : SQLite; 
    public log : string = ""; 
    constructor(public http: Http) { 
    console.log('Hello SqliteHelper Provider'); 
    } 

    public initDb() { 
    this.db = new SQLite(); 
    this.log += "openDatabase。。。"; 
    // if (Device.device.platform) 
    this.db.openDatabase({ 
     name: "data.db", 
     location: "default" 
    }).then((data) =>{ 
     this.log += ("open ok " + JSON.stringify(data)); 
    }, (err) => { 
     this.log += ("open err " + err.message + " " + JSON.stringify(err)); 
    }); 
    } 

    public executeSql(statement: string, parms:any) { 
    return this.db.executeSql(statement, parms); 
    } 

} 

し、init sqlitehelper::私はprovierとしてSQLiteのラッパー

constructor(platform: Platform, sqliteHelper : SqliteHelper, events: Events) { 
    platform.ready().then(() => { 
     // Okay, so the platform is ready and our plugins are available. 
     // Here you can do any higher level native things you might need. 
     sqliteHelper.initDb(); 
     events.publish("sqlite:inited", null); 
     StatusBar.styleDefault(); 
     Splashscreen.hide(); 
    }); 
    } 

そして、私はplatform.readyとコンストラクタで最初のタブページからデータをロードし、アンドロイドの意志でそれを実行します原因エラー:未定義のexecuteSqlプロパティを読み取ることができません。

ボタンをクリックしてデータを読み込むと問題ありません。ロードデータを2ページ目のコンストラクタに置いても大丈夫です。誰が助けてくれるのでしょうか?最初のページにコードを入れ、ページをロードするときにデータをロードします。

答えて

1

私はトランザクション内で「は、ExecuteSQL」機能を追加したことを理由データベースに挿入しようとすると私は同じエラーが発生しました:

 this.database.openDatabase({ 
         name: "sis.db", 
         location: "default" 
        }).then(() => { 

this.database.executeSql("CREATE TABLE IF NOT EXISTS profile(id integer primary key autoincrement NOT NULL ,name Text NOT NULL)", []).then((data) => { console.log('profile table created'); }, (error) => { console.log('Unable to create table profile'); }) 

this.database.transaction(tr => { tr.executeSql("insert into profile(id,name) values(12,'Ibrahim')", []); }).then(d => { console.log('Data inserted ya hima'); }, err => { console.error('unable to insert data into profile table'); }); 
         this.database.executeSql("select id from profile", []).then(d => { console.log('inserted id=' + d.rows.item(0).app_id); }, err => { console.error('unable to get ID from profile table'); }); 

         }, (error) => {console.error(error); } 
         ); 
関連する問題