带有in运算符的Ionic3原生SQLite选择查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带有in运算符的Ionic3原生SQLite选择查询相关的知识,希望对你有一定的参考价值。

我在我的ionic-3应用程序中使用SQLite本机插件,当我试图查询以获得学生一些学生时查询不起作用。这是空洞的结果。

//studentIds array
let studentIds = [1,2,4,5];
//Query part
this.database.executeSql("Select * from student where id in (?)", [studentIds]).then(data => {
  let students = [];
  for(let i=0; i < data.rows.length; i++) {
    students.push({id: data.rows.item[i].id, name: data.rows.item[i].name});
  }
  return students;
});
答案

这在Ionic 3中适用于我:

//studentIds array
let studentIds = [1,2,4,5];

//Query part
this.database.executeSql(`select * from student where id in (?)`, [studentIds]).then(data => {

  let students = [];

  for(let i=0; i < data.rows.length; i++)
    students.push({id: data.rows.item(i).id, name: data.rows.item(i).name});

  return students;
});

问题是在[]data.rows.item[i],你必须像这样()使用data.rows.item(i)

另一答案

对于创建数据库

   this._platform.ready().then(() => {
               this.sqlite = new SQLite();
               this.sqlite.create({name: "mymain.db", location: "default"}).then((db: SQLiteObject) => {
                    db.executeSql('CREATE TABLE IF NOT EXISTS mydata (allvalues TEXT)', {})
                    .then(() => console.log('Executed SQL'))
                    .catch(e => console.log(e));
                })
                .catch(e => console.log(e));
        });
 }

用于将数据添加到sqlite

public saveTablicaToSqlite(DataArray){
  this.sqlite.create({
  name: 'mymain.db',
  location: 'default'
  })
  .then((db: SQLiteObject) => {
       db.executeSql("INSERT INTO mydata (allvalues) VALUES (?)", [DataArray]).then((data) => {
       console.log("INSERTED: " + JSON.stringify(data));
       }, (error) => {
       console.log("ERROR: " + JSON.stringify(error.err));
    });
  })
 .catch(e => console.log(e));
}

用于获取数据

fetchdata(datavalue,splashScreen){
console.info('fetch',datavalue);
return new Promise((resolve, reject) => {
  this.sqlite.create({
  name: 'mymain.db',
  location: 'default'
  })
  .then((db: SQLiteObject) => {
    db.executeSql("SELECT * FROM mydata", []).then((data) => {
        if(data.rows.length > 0) {
            for(let i = 0; i < 1; i++) {
              var obj = JSON.parse(data.rows.item(i).allvalues);
              console.info(obj);

以上是关于带有in运算符的Ionic3原生SQLite选择查询的主要内容,如果未能解决你的问题,请参考以下文章

房间 - 选择带有 IN 条件的查询?

uniapp 原生sqlite本地数据库管理 Ba-Sqlite

uniapp 原生sqlite本地数据库管理 Ba-Sqlite

ionic3项目中的'SQLite'类型上不存在错误属性'openDatabase'?

媒体捕捉原生插件 Ionic 3

Node.js sqlite3 IN 运算符