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

问题描述 投票:0回答:2

我在我的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;
});
sqlite cordova ionic-framework ionic3 ionic-native
2个回答
1
投票

这在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)


0
投票

对于创建数据库

   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);
© www.soinside.com 2019 - 2024. All rights reserved.