VenomVendor
VenomVendor

Reputation: 15382

Get list of tables from SQLite in Node.js

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);
    });
});

Output

{name: "meta"}

When opened in sqlite3 command-line

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

Upvotes: 9

Views: 12396

Answers (1)

Shanoor
Shanoor

Reputation: 13662

From the get()'s doc:

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

You have to use db.all():

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

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);
    });
});

Upvotes: 15

Related Questions