什么SQL可以用来列出表,以及在SQLite数据库文件中的这些表中的行-一旦我已经附加了它与SQLite 3命令行工具上的ATTACH命令?


当前回答

在SQLite命令行中有一个命令可用:

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

它转换为以下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

其他回答

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

SELECT name FROM sqlite_master WHERE type='table'

在iOS中使用:

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

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

通过union all,将所有表合并到一个列表中。

select name
from sqlite_master 
where type='table'

union all 

select name 
from sqlite_temp_master 
where type='table'

在SQLite命令行中有一个命令可用:

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

它转换为以下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

.tables和.schema“助手”函数不查找ATTACHed数据库:它们只查询SQLITE_MASTER表以查找“主”数据库。因此,如果你使用

ATTACH some_file.db AS my_db;

然后你需要做

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

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

SELECT name FROM sqlite_temp_master WHERE type='table';