Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of tables from SQLite in Node.js

Tags:

node.js

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

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
like image 377
VenomVendor Avatar asked Dec 27 '15 13:12

VenomVendor


1 Answers

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);
    });
});
like image 71
Shanoor Avatar answered Sep 25 '22 05:09

Shanoor