0
0

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-11-01

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を使う場合

pragma table_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では動作しませんでした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0