2016-11-16 19 views
2

我写了一个测试选项卡ionic 2 app使用sqlite插件。我件中的SQLite作为provier:离子2 sqlite在android上运行:无法读取属性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在app.components:

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构造函数加载从第一个标签页的数据,对Android将运行导致err:无法读取未定义的属性executeSql。

如果我从按钮点击加载数据,没关系。或者我把loaddata放到第二页的构造函数中,这也没关系。为什么?谁能帮助我,我想把代码放到第一页并在页面启动时加载数据。

回答

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