在 SQLite 中使用 SELECT 命令列出附加数据库
是否有一个 SELECT 命令可以列出所有附加的数据库,类似于 sqlite3 中可用的 .database 命令?
Is there a SELECT command that can list all attached databases similar to the .database command available in sqlite3?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,您无法使用 SELECT 语句来执行此操作(尽管您可能想在
main
数据库中查看,该数据可能存储在那里)。不过,有一个解决方案。如果执行以下语句,它将返回当前连接附加的数据库:第一行始终是主数据库,第二行始终是临时数据库。任何其他数据库都位于前两个数据库之后。您可以针对数据库运行此语句,就像在 C# 代码中运行 SELECT 语句(或任何其他相关内容)一样。
这是一个很好的参考:
SQLite PRAGMA 语句参考
祝你好运!
You cannot do this with a SELECT statement that I know of (though you might want to look around in the
main
database, this data might be stored there). However, there is a solution. If you execute the following statement it will return the databases attached for the current connection:The first row will always be the main database, the second will always be the temp database. Any further databases are after these first two. You can run this statement against your database the same way you would a SELECT statement from your code in c# (or anything else for that matter).
Here is a good reference:
SQLite PRAGMA statement reference
Good luck!
发布时接受的答案是正确的,但在 SQLite 3.16.0 及更高版本中,大多数无副作用的编译指示也可以作为所谓的 编译指示函数。
这意味着您可以编写:
.headers on
是完全可选的,但非常有用,因为这些编译指示函数返回的列名称没有记录在任何地方。但请注意,它们未记录的部分原因是“此功能是实验性的,可能会发生变化”。
The accepted answer was correct when it was posted, but in SQLite 3.16.0 and later most of the side-effect free pragmas can also be accessed as so called pragma functions.
Which means you can write:
.headers on
is completely optional, but very useful, since the column names these pragma functions return are not documented anywhere.Be aware though, part of the reason they are undocumented is 'This feature is experimental and is subject to change'.
下面的命令可以列出附加的数据库:
This command below can list attached databases: