从 Node.js 中的 SQLite 获取表列表 [英] Get list of tables from SQLite in Node.js

查看:24
本文介绍了从 Node.js 中的 SQLite 获取表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码只返回第一个表的名称,如何获取现有sqlite中所有可用表名的列表?

Below code returns only the name of first table, how to get list of all available table names in existing sqlite?

const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('path/to/mydb.sqlite');
db.serialize(function () {
    db.get("select name from sqlite_master where type='table'", function (err, table) {
        console.log(table);
    });
});

输出

{name: "meta"}

在 sqlite3 命令行中打开时

When opened in sqlite3 command-line

sqlite> .tables
downloads             meta                  urls
downloads_url_chains  segment_usage         visit_source
keyword_search_terms  segments              visits

推荐答案

来自 get() 的文档:

使用指定的参数运行 SQL 查询,然后使用 第一个结果行调用回调.

Runs the SQL query with the specified parameters and calls the callback with the first result row afterwards.

你必须使用 db.all():

db.serialize(function () {
    db.all("select name from sqlite_master where type='table'", function (err, tables) {
        console.log(tables);
    });
});

或者 db.each() 如果要为每一行调用回调:

Or db.each() if you want to call the callback for every row:

db.serialize(function () {
    db.each("select name from sqlite_master where type='table'", function (err, table) {
        console.log(table);
    });
});

这篇关于从 Node.js 中的 SQLite 获取表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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