1

我刚刚尝试连接我的本机脚本应用程序。在 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)

有人能帮忙吗?

4

1 回答 1

0

当我在模拟器中搜索更多信息时,会出现“execSql 不是函数”之类的错误。所以我将它更改为 execSQL,就像我使用顶部一样,现在它工作正常。

于 2017-05-08T12:24:44.790 回答