【问题标题】:Reading the results of SQLite SELECT query in Ionic2 v3.4在 Ionic2 v3.4 中读取 SQLite SELECT 查询的结果
【发布时间】:2017-12-04 15:23:04
【问题描述】:

所以我正在使用最新版本的 Ionic2 (v3.4),并且我正在尝试让离子原生 SQLite 工作。我已经能够像这样创建数据库文件并在其中放置一个表:

this.sqlite.create({
  name: "data.db",
  location: "default"
})
.then((db:SQLiteObject) => {
  db.executeSql(tableQuery, {})
  .then(() => console.log("success"))
  .catch(() => console.log("fail"));
})

插入也可以。但是当我尝试获得选择的结果时:

this.sqlite.create({
  name: "data.db",
  location: "default"
})
.then((db:SQLiteObject) => {
  db.executeSql("SELECT * FROM savedCoupons where itemId=" + itemId, {})
  .then((db) => {console.log(JSON.stringify(db))})
  .catch(() => console.log("***ERROR WITH SELECT***"));
})
.catch(() => console.log("ERROR: FAILED TO CREATE/OPEN DATABASE."));

由于缺少文档,我迷路了。 JSON.stringify() 正在运行,因此查询似乎有效。它返回{"rows":{"length":1}, "rowsAffected":0},就是这样。如何访问查询结果?

【问题讨论】:

  • 您好,我也遇到了同样的问题,解决了吗?

标签: android sqlite typescript ionic2


【解决方案1】:

让您的表中有三列 e.x id,name,lastname。 查询后您可以像这样访问它:

  db.executeSql('SELECT * FROM student WHERE id='+1, {})
          .then(result => {
            for (var i = 0; i < result.rows.length; i++) {
               console.log("---Id---"+result.rows.item(i).id);
               console.log("---Name---"+result.rows.item(i).name);
               console.log("---Lastname---"+result.rows.item(i).lastname);
           }
          });

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    相关资源
    最近更新 更多