我刚尝试连接我的Native-script app.In app.component.ts文件我创建了我的数据库和两个方法。
export class AppComponent {
private database: any;
private people: Array<any>;
public constructor() {
console.log(" database service constructors");
(new Sqlite("my.db")).then(db => {
db.execSQL("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY AUTOINCREMENT, firstName TEXT, lastName Text)").then(db => {
this.database = db;
}, error => {
console.log("CREATE TABLE ERROR", error);
})
}, error => {
console.log("CREATE DB ERROR", error);
})}
public insert() {
console.log("Acess Insert Ok");
this.database.execSql("INSERT INTO people (firstName, lastName) VALUES (?,?)", ["Randika", "Perera"]).then(id => {
console.log("Insert Ok");
this.fetch();
}, error => {
console.log("INSERT ERROR: ", error);
})}
public fetch() {
this.database.all("SELECT * FROM people").then(rows => {
this.people = [];
for (let row in rows) {
this.people.push({
"id": rows[row][0],
"firstName": rows[row][1],
"lastName": rows[row][2]
})
}
}, error => {
console.log("SELECTOR ERROR:", error);
});}}
在app.component.hlml文件中:
<ActionBar title="My App">
<ActionItem text="Add" (tap)="insert()"></ActionItem>
但是点击添加按钮后会出现错误,如
W/System.err(10737): at com.tns.Runtime.callJSMethodNative(Native Method)
W/System.err(10737): at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1197)
W/System.err(10737): at com.tns.Runtime.callJSMethodImpl(Runtime.java:1061)
W/System.err(10737): at com.tns.Runtime.callJSMethod(Runtime.java:1047)
W/System.err(10737): at com.tns.Runtime.callJSMethod(Runtime.java:1028)
W/System.err(10737): at com.tns.Runtime.callJSMethod(Runtime.java:1018)
任何人都可以帮忙吗?