【问题标题】:How to format .schema output with SQLite3?如何使用 SQLite3 格式化 .schema 输出?
【发布时间】:2016-11-10 09:29:41
【问题描述】:

我使用 python 创建了一些 sqlite 表架构。

当我使用 sqlite3 客户端并发出 .schema 命令时,我可以看到输出已预先格式化为我用作 executescript() 函数参数的 SQL 源文件。

有什么方法可以用来格式化.schema 命令的输出吗?

【问题讨论】:

  • "SQL 应有的样子" 这是什么意思?
  • 缩进,关键字大写...这纯粹是为了美观。我知道我能做的最好的事情就是自己很好地格式化 SQL,但我也问这个表声明格式错误的情况,当我发出 .schema 命令。
  • 您是要我们重新格式化 SQL 的代码,还是要一些库或工具?
  • 我想知道是否有任何方法可以直接在 sqlite3 客户端中执行此操作,但我也对任何工具或库 (python3) 感兴趣。

标签: sql python-3.x sqlite database-schema


【解决方案1】:

在最新版本 ( 3.13.0 ) 中,您有

>sqlite3 test.dat
SQLite version 3.13.0 2016-05-18 10:57:30

sqlite> create table test ( a integer,b integer, c integer,d float,e text, f primary key);

sqlite> .schema
CREATE TABLE test ( a integer,b integer, c integer,d float,e text, f primary key);

sqlite> .schema --indent
CREATE TABLE test(
  a integer,
  b integer,
  c integer,
  d float,
  e text,
  f primary key
);

【讨论】:

  • 谢谢。似乎在我的发行版中被破坏了,但我稍后会看看。
  • 工作正常。也许是因为我重新创建了数据库文件?它不适用于视图,但它很酷。
【解决方案2】:

我也在寻找解决方案。
命令:

> .header on
> .mode column
> pragma table_info('table_name');

给出输出:

    cid         name        type         notnull     dflt_value  pk
----------  ----------  -----------  ----------  ----------  ----------
0           id          varchar(24)  1                       1
1           uuid        varchar(36)  1                       0
2           title       varchar(200  1                       0
3           slug        varchar(191  1                       0

【讨论】:

  • 或者你可以像这样运行 sqlite:sqlite3 --column --header
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 2020-11-14
  • 1970-01-01
相关资源
最近更新 更多