How to list the tables in a SQLite database file t

2018-12-31 15:46发布

What SQL can be used to list the tables, and the rows within those tables in a SQLite database file - once I have attached it with the ATTACH command on the SQLite 3 command line tool??

17条回答
梦醉为红颜
2楼-- · 2018-12-31 16:25

Try PRAGMA table_info(table-name);
http://www.sqlite.org/pragma.html#schema

查看更多
妖精总统
3楼-- · 2018-12-31 16:26

To show all tables, use

SELECT name FROM sqlite_master WHERE type = "table"

To show all rows, I guess you can iterate through all tables and just do a SELECT * on each one. But maybe a DUMP is what you're after?

查看更多
忆尘夕之涩
4楼-- · 2018-12-31 16:27

Use .help to check for available commands.

.table

This command would show all tables under your current database.

查看更多
弹指情弦暗扣
5楼-- · 2018-12-31 16:31

The easiest way to do this is to open the database directly and use the .dump command, rather than attaching it after invoking the SQLite 3 shell tool.

So... (assume your OS command line prompt is $) instead of $sqlite3:

sqlite3> ATTACH database.sqlite as "attached"

From your OS command line, open the database directly:

$sqlite3 database.sqlite
sqlite3> .dump
查看更多
冷夜・残月
6楼-- · 2018-12-31 16:33

The .tables, and .schema "helper" functions don't look into ATTACHed databases: they just query the SQLITE_MASTER table for the "main" database. Consequently, if you used

ATTACH some_file.db AS my_db;

then you need to do

SELECT name FROM my_db.sqlite_master WHERE type='table';

Note that temporary tables don't show up with .tables either: you have to list sqlite_temp_master for that:

SELECT name FROM sqlite_temp_master WHERE type='table';
查看更多
泛滥B
7楼-- · 2018-12-31 16:33

To list the tables you can also do:

SELECT name FROM sqlite_master
WHERE type='table';
查看更多
登录 后发表回答