我如何从 Sqlite3 获取 Json 输出 [英] how can i get Json output from Sqlite3

查看:33
本文介绍了我如何从 Sqlite3 获取 Json 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我写了一个 SQL 来从 2 个表中获取信息,但是当我想返回数据时,我不知道在正确的时间进行索引.我应该怎么做,无论如何要从第一个而不是每个方法获得 Json 响应?

Hello i wrote a SQL for getting information from 2 tables but when i want to return the data i don't know the index to do it at the right moment. what should i do and is there anyway to get Json response from the first instead of each method?

我的功能:

const searchAntibodies = (
  index: number,
  amount: number,
  information: string,
  startDate: string,
  endDate: string,
) => {
  return new Promise<Antibodies[]>((resolve, reject) => {
    let antibodies: Antibodies[] = [];
    db.serialize(() => {
      db.each(`SELECT id, name as antibodyName FROM Antibodies WHERE
              id IN 
              (SELECT id FROM Antibodies WHERE name LIKE ?
              UNION all
              SELECT antiId FROM AssignedColors WHERE name LIKE ?
              UNION all
              SELECT antiId FROM AssignedReactivities WHERE name LIKE ?)
              AND dateOfCreation >= ? AND dateOfCreation <= ?
              ORDER BY dateOfCreation DESC LIMIT ? OFFSET ?;`
        , [`%${information}%`, `%${information}%`, `%${information}%`, startDate, endDate, amount, index]
        , (err, antibody: Antibodies) => {
          if (err) {
            reject(err.message);
          } else {
            db.all('SELECT name, locations, colorId FROM AssignedColors WHERE antiId = ?', [antibody.id], (err, colors) => {
              if (err) {
                reject(err.message);
              } else {
                antibody.colors = colors;
                antibodies.push(antibody);
                if (antibodies.length === 10) {
                  resolve(antibodies)
                }
              }
            });
          }
        });
    });
  });
}

我的预期结果:

[   {
    id: 1999,
    antibodyName: 'Antibody 1999',
    colors: [ [Object], [Object], [Object], [Object], [Object] ]   },   {
    id: 1995,
    antibodyName: 'Antibody 1995',
    colors: [ [Object], [Object], [Object], [Object], [Object] ]   },   {
    id: 1994,
    antibodyName: 'Antibody 1994',
    colors: [ [Object], [Object], [Object], [Object], [Object] ]   },   {
    id: 1993,
    antibodyName: 'Antibody 1998',
    colors: [ [Object], [Object], [Object], [Object], [Object] ]   },   {
    id: 1997,
    antibodyName: 'Antibody 1997',   } ]

推荐答案

好吧,搜索了很多找到了答案.实际上,如果您不为第一个回调函数设置类型,则可以在完成时获得第二个回调函数.这是我的结果:

Ok after searching a lot found the answer. Actually if you don't set types for the first callback function, you can get a second callback function for when it is completed. Here is my result:

const getAntibodies = (
  index: number,
  amount: number,
  startDate: number,
  endDate: number,
  orderBy: string,
  orderType: string,
) => {
  return new Promise<Antibodies[]>((resolve, reject) => {
    let antibodies: Antibodies[] = [];
    let totalCount: number;
    let sql = 'SELECT id, name as antibodyName, dateOfCreation FROM Antibodies ';
    let params = [amount, index];
    if (startDate !== 0 || endDate !== 0) {
      sql += `WHERE dateOfCreation >= ? AND dateOfCreation <= ? 
                ORDER BY ${orderBy} ${orderType} LIMIT ? OFFSET ?;`;
      params.unshift(startDate, endDate);
    } else {
      sql += `ORDER BY ${orderBy} ${orderType} LIMIT ? OFFSET ?;`;
    }
    db.serialize(() => {
      db.each(sql,
        params
        , async (err, antibody) => {
          if (err) {
            reject(err.message);
          } else {
            await getColors(antibody.id).then((colors) => {
              antibody.colors = colors;
              antibodies.push(antibody);
              if (antibodies.length === totalCount) {
                resolve(antibodies);
              }
            }).catch((err) => {
              reject(err);
            });
          }
        }, (err, count) => {
          if (err) {
            reject(err.message)
          } else {
            if (count === 0) {
              resolve(antibodies);
            } else {
              totalCount = count;
            }
          }
        });
    });
  });
}

const getColors = (id: number) => {
  return new Promise<Color[]>((resolve, reject) => {
    db.serialize(() => {
      db.all('SELECT name, locations, colorId FROM AssignedColors WHERE antiId = ?', [id], (err, colors: Color[]) => {
        if (err) {
          reject(err.message)
        } else {
          resolve(colors);
        }
      });
    });
  });
}

这篇关于我如何从 Sqlite3 获取 Json 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆