SQL 如何列出使用 ATTACH 打开的 SQLite 数据库文件中的表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/82875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 23:27:03  来源:igfitidea点击:

How to list the tables in a SQLite database file that was opened with ATTACH?

sqldatabasesqlitemetadata

提问by izb

What SQLcan be used to list the tables, and the rows within those tables in an SQLitedatabase file - once I have attached it with the ATTACHcommand on the SQLite 3command line tool?

什么SQL可用于列出SQLite数据库文件中的表以及这些表中的行- 一旦我ATTACHSQLite 3命令行工具上使用命令附加了它?

采纳答案by Anthony Williams

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

.tables.schema“辅助”功能不考虑附加的数据库:他们只是查询SQLITE_MASTER表“主”数据库。因此,如果您使用

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 .tableseither: you have to list sqlite_temp_masterfor that:

请注意,临时表也不会显示.tables:您必须列出sqlite_temp_master

SELECT name FROM sqlite_temp_master WHERE type='table';

回答by Mark Janssen

There are a few steps to see the tables in an SQLite database:

查看 SQLite 数据库中的表有几个步骤:

  1. List the tables in your database:

    .tables
    
  2. List how the table looks:

    .schema tablename
    
  3. Print the entire table:

    SELECT * FROM tablename;
    
  4. List all of the available SQLite prompt commands:

    .help
    
  1. 列出数据库中的表:

    .tables
    
  2. 列出表格的外观:

    .schema tablename
    
  3. 打印整个表:

    SELECT * FROM tablename;
    
  4. 列出所有可用的 SQLite 提示命令:

    .help
    

回答by Lasse V. Karlsen

It appears you need to go through the sqlite_mastertable, like this:

看来您需要遍历sqlite_master表,如下所示:

SELECT * FROM dbname.sqlite_master WHERE type='table';

And then manually go through each table with a SELECTor similar to look at the rows.

然后手动遍历每个表SELECT以查看行。

The .DUMPand .SCHEMAcommands doesn't appear to see the database at all.

.DUMP.SCHEMA命令不会出现看到数据库的。

回答by Christian Davén

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?

要显示所有行,我想您可以遍历所有表,然后对每个表执行 SELECT * 。但也许 DUMP 就是你所追求的?

回答by Antony.H

Use .helpto check for available commands.

使用.help检查可用的命令。

.table

This command would show all tables under your current database.

此命令将显示当前数据库下的所有表。

回答by Antony.H

There is a command available for this on the SQLite command line:

SQLite 命令行上有一个可用的命令:

.tables ?PATTERN?      List names of tables matching a LIKE pattern

Which converts to the following SQL:

这将转换为以下 SQL:

SELECT name FROM sqlite_master
WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'
UNION ALL
SELECT name FROM sqlite_temp_master
WHERE type IN ('table','view')
ORDER BY 1

回答by Rafa? Dowgird

To list the tables you can also do:

要列出表格,您还可以执行以下操作:

SELECT name FROM sqlite_master
WHERE type='table';

回答by Luiz Geron

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

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

回答by GameLoading

I use this query to get it:

我使用这个查询来获取它:

SELECT name FROM sqlite_master WHERE type='table'

And to use in iOS:

并在 iOS 中使用:

NSString *aStrQuery=[NSString stringWithFormat:@"SELECT name FROM sqlite_master WHERE type='table'"];

回答by Alix Axel

According to the documentation, the equivalent of MySQLs' SHOW TABLES;is:

根据文档,MySQLs 的等价物SHOW TABLES;是:

The ".tables" command is similar to setting list mode then executing the following query:

“.tables”命令类似于设置列表模式然后执行以下查询:

SELECT name FROM sqlite_master
  WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'
UNION ALL
SELECT name FROM sqlite_temp_master
  WHERE type IN ('table','view')
ORDER BY 1;

However, if you are checking if a single table exists (or to get its details), see @LuizGeronanswer.

但是,如果您要检查单个表是否存在(或获取其详细信息),请参阅@LuizGeron答案。