2016-09-30 45 views
0

我正在使用Ionic 2,並且遵循this tutorialIonic 2 SQLite未打開數據庫

我的問題是我似乎無法打開數據庫。我已將它部署到名爲KOPLAYER的Android模擬器。

app.ts

initializeApp() { 
    this.platform.ready().then(() => { 
     StatusBar.styleDefault(); 
     if (window.cordova) { 
     this.createDatabase(); 
     } 
    }); 
    } 

private createDatabase(): void { 
    let db: SQLite = new SQLite(); 
    db.openDatabase({ 
     name: "data.db", 
     location: "default" 
    }).then(() => { 
     db.executeSql("CREATE TABLE IF NOT EXISTS chats (_id TEXT PRIMARY KEY, memberIds TEXT, title TEXT, subTitle TEXT, picture TEXT, lastMessageId TEXT, lastMessageCreatedAt DATE)", {}).then((chatData) => { 
     console.log("chats TABLE CREATED: ", chatData); 
     db.executeSql("CREATE TABLE IF NOT EXISTS messages (_id TEXT PRIMARY KEY, chatId TEXT, senderId TEXT, ownership TEXT, content TEXT, createdAt DATE, changeDate BOOLEAN, readByReceiver BOOLEAN)", {}).then((messageData) => { 
      console.log("messages TABLE CREATED: ", messageData); 
     }, (error) => { 
      console.error("Unable to execute messages sql", error); 
     }); 

     }, (error) => { 
     console.error("Unable to execute chats sql", error); 
     }); 
    }, (error) => { 
     console.error("Unable to open database", error); 
    }); 
    } 

storageService.ts

public database: SQLite; 

constructor() { 
    if (window.cordova) { 
     this.openDatabase(); 
    } 
} 

private openDatabase(): void { 
    console.log('openDatabase'); 
    this.database.openDatabase({ name: "data.db", location: "default" }).then(() => { 
     this.refreshChats(); 
     this.refreshMessages(); 
    }, (error) => { 
     console.log("ERROR: ", error); 
    }); 
} 

openDatabase被輸出到控制檯,但this.refreshChats();不被調用。根據控制檯日誌,app.ts中的數據庫創建看起來是正確的。

OPEN database: data.db SQLitePlugin.js:175 
OPEN database: data.db - OK SQLitePlugin.js:179 
chats TABLE CREATED: Object app.bundle.js:215 
messages TABLE CREATED: Object app.bundle.js:217 

然後

openDatabase

我使用chrome://inspect/#devices來查看控制檯輸出。有沒有其他工具可以用來查看模擬器上運行的數據庫?

任何幫助表示讚賞。

回答

0

我的錯誤,我忘了this.database = new SQLite();

private openDatabase(): void { 
    this.database = new SQLite(); 
    this.database.openDatabase({ name: "data.db", location: "default" }).then(() => { 
     this.refreshChats(); 
     this.refreshMessages(); 
    }, (error) => { 
     console.log("ERROR: ", error); 
    }); 
}