【问题标题】:ionic 3: How to use storage(Sqlite) pluginionic 3:如何使用存储(Sqlite)插件
【发布时间】:2017-06-17 20:36:30
【问题描述】:

我已经创建了登录屏幕和主屏幕我有一个包含三个数据的对象,这些数据必须使用 sqlite 来存储我不知道该怎么做

{
      "userId": "",
      "loginId": "",
      "passwd": ""
    }

我创建了一个提供程序,用于存放我的 sqlite 商店代码

export class SqlStorage {

  constructor(public http: Http,private sqlite: SQLite) {
    console.log('Hello SqlStorage Provider');

    this.sqlite.create({
      name: 'data.db',
      location: 'default'
    })
    .then((db: SQLiteObject) => {
      db.executeSql('create table kmartIndia(name VARCHAR(32))', {})
      .then((db) => { 
        console.log('Executed SQL');
      })
      .catch((e) => {
        console.log(e)
      });
    })
    .catch((e) => {
      console.log(e)
    });
  }

  setValue(){}

  getValue(){}

  removeValue(){}
}

我不知道如何让它工作

如何创建表以及如何设置、获取和删除值我对此不是很熟悉任何帮助。

【问题讨论】:

  • @mayur 我看到一个我无法理解它在哪里创建以及如何将对象设置到表中
  • stackoverflow.com/questions/40277905/… 你在这里,检查一下,谢谢!
  • @mosca90 我不明白你的第 6 点
  • 这是您从 .ts 页面调用 sql 提供程序的方式! @Mohan Gopi

标签: sqlite angular ionic2 storage ionic3


【解决方案1】:

经过一番挖掘,我发现了如何使这项工作。

好像“create”方法是用来创建和打开数据库的。

在你的 setValue() 中做:

this.sqlite.create({
  name: 'data.db',
  location: 'default'
}).then((db: SQLiteObject) => {
  db.executeSql("insert into kmartIndia(name) values ('Mohan')", {}).then(() => yourVariable = 'Executed SQL').catch(e => yourVariable = e);
}).catch(e => console.log(e));

并且,从表中获取数据:

this.sqlite.create(
  {name: 'data.db', location: 'default'}
  ).then(
    (db: SQLiteObject) => {
      db.executeSql('select * from kmartIndia', {}).then( 
        (data) => {
          for(var i =0; i< data.rows.length;i++){
            yourVariable += data.rows.item(i).name
          }
        } 
      )
    }
  );

(我用 += 分配了值,但这只是示例:))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 2018-08-06
    • 2018-10-10
    • 2023-03-15
    • 2018-02-23
    • 1970-01-01
    相关资源
    最近更新 更多