9

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

1 Answer 1

15

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);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.