Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8896

NodejsのSQLiteでカラム名の一覧を取得する方法

$
0
0

https://stackoverflow.com/questions/947215/how-to-get-a-list-of-column-names-on-sqlite3-database
こちらのサイトを参考にしました。

データの確認にはDB Browser for SQLiteを使っていたのですが、DB Browser for SQLite上でSQLを実行するときと、Node.js上のSQLiteモジュールを通して実行したときとで実行できるものが違うようです。

DB Browser for SQLiteを使う場合

pragmatable_info(tablename)

で指定したテーブルのカラム名の一覧が取得できます。
他のSELECTなどのクエリと一緒に使うことはできないようです。

Nodejsで使う場合

db.each("SELECT name FROM pragma_table_info('users')", function(err, args) {
    console.log(args['name']);
});

でも取得できるようです。
pragma_table_infoはDB Browser for SQLiteでは動作しませんでした。


Viewing all articles
Browse latest Browse all 8896

Trending Articles