简体   繁体   English

从 Node.js 中的 SQLite 数据库中的不同表中获取所有数据

[英]Get all data from different tables in a SQLite database in Node.js

Is it a way to get all data from a database?这是一种从数据库中获取所有数据的方法吗?

So far I accomplished to get all data from a table:到目前为止,我完成了从表中获取所有数据:

router.get('/', function (req, res, next) {
  db.serialize(function () {
    db.all('SELECT id, name, surname FROM Table1', function (
      err,
      rows
    ) {
      return res.send(rows);
    });
  });

  db.close();
});

but if there are more tables, Table1 , Table2 and Table3 for example.但是如果有更多的表,例如Table1Table2Table3

Trying to put all the queries in the db.all call doesn't seem to work.尝试将所有查询放在 db.all 调用中似乎不起作用。

 router.get('/', function (req, res, next) {
      db.serialize(function () {
        db.all('SELECT id, name, surname FROM Table1, SELECT date, city FROM Table2, SELECT person, year FROM Table3', function (
          err,
          rows
        ) {
          return res.send(rows);
        });
      });
    
      db.close();
    });

UPDATE更新

these are the tables, how can I get all their data and send it into front-end?这些是表格,我怎样才能获取他们的所有数据并将其发送到前端? that's what I want to do.这就是我想做的。

I would rather want to have them like separate tables in the front-end where the call is fetched like this:我宁愿让它们像前端中的单独表一样获取调用,如下所示:

callAPI() {
    fetch('http://localhost:9000/testAPI')
      .then((res) => res.text())
      .then((res) => this.setState({ apiResponse: res }));
  }

is there a way to send all these tables?有没有办法发送所有这些表格?

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

Provided that all your tables have the same columns or at-least the output can be aliased into same column names (maybe put nulls if the column is not in one of the tables), you can make use of UNION如果您的所有表都具有相同的列或至少 output 可以别名为相同的列名(如果该列不在其中一个表中,则可能放置空值),您可以使用UNION

eg:例如:

SELECT id, name FROM table1 
 UNION 
   SELECT id, name FROM table2
 UNION 
   SELECT myid as id, test as name FROM table3

This way you can append all such tables into a single output table.这样,您可以将 append 所有此类表合并到单个 output 表中。 Also if you want it to aggregate through code, you can use the below query to get all table names and work with each table:此外,如果您希望它通过代码进行聚合,您可以使用以下查询来获取所有表名并使用每个表:

SELECT name FROM my_db.sqlite_master WHERE type='table';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM