AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 40656
Accepted
Kshitiz Sharma
Kshitiz Sharma
Asked: 2013-04-23 21:24:46 +0800 CST2013-04-23 21:24:46 +0800 CST 2013-04-23 21:24:46 +0800 CST

如何正确格式化 sqlite shell 输出?

  • 772

如果我去mysql shell打字,SELECT * FROM users我会得到 -

+--------+----------------+---------------------------------+----------+-----------+--------------------+--------------------+
| USERID | NAME           | EMAILID                         | PASSWORD | USER_TYPE | CONTACT_ID_FKUSERS | COMPANY_ID_FKUSERS |
+--------+----------------+---------------------------------+----------+-----------+--------------------+--------------------+
|    137 | X              | [email protected]                        | #        | ADMIN     |                166 |                110 |
|    138 | Kshitiz        | [email protected]                 | asdf     | ADMIN     |                167 |                111 |
+--------+----------------+---------------------------------+----------+-----------+--------------------+--------------------+

Oracle sqlplus显示 -

USERID     NAME  EMAILID    PASSWORD   USER_TYPE  CONTACT_ID_FKUSERS COMPANY_ID_FKUSERS
---------- ----- ---------- ---------- ---------- ------------------ ------------------
137        X     [email protected]   #          ADMIN                     166                110
137        X     [email protected]   #          ADMIN                     166                110

Sqlite shell显示 -

137|X|[email protected]|#|ADMIN|166|110
138|Kshitiz|[email protected]|asdf|ADMIN|167|111
  1. 有没有办法美化输出sqlite shell?
  2. 是否有比默认发行版更好的替代外壳?(仅限 CLI 客户端)
sqlite
  • 9 9 个回答
  • 91651 Views

9 个回答

  • Voted
  1. Best Answer
    Mat
    2013-04-24T01:39:37+08:002013-04-24T01:39:37+08:00

    对于“人类可读”输出,您可以使用column模式,并打开标题输出。这将为您提供类似于sqlplus示例中的输出的内容:

    sqlite> select * from foo;
    234|kshitiz|dba.se
    
    sqlite> .mode column
    sqlite> select * from foo;
    234         kshitiz     dba.se
    
    sqlite> .headers on
    sqlite> select * from foo;
    bar         baz         baf
    ----------  ----------  ----------
    234         kshitiz     dba.se
    
    • 178
  2. miken32
    2018-10-13T10:17:48+08:002018-10-13T10:17:48+08:00

    所有答案都提供了您可以在 SQLite 控制台或通过 CLI 键入的设置,但没有人提到可以将这些设置放入 RC 文件中以避免一直键入它们。另存为~/.sqliterc:

    .mode column
    .headers on
    .separator ROW "\n"
    .nullvalue NULL
    

    注意我还为空值添加了一个占位符,而不是默认的空字符串。

    • 48
  3. jersey bean
    2016-08-09T15:49:30+08:002016-08-09T15:49:30+08:00

    对于那些对获得相同结果感兴趣的人,除了从命令行运行 sqlite。我发现以下不起作用:

    $ sqlite3 <dbfile> ".headers on;.mode column;select * from MyTable"
    Error: mode should be one of: ascii column csv html insert line list tabs tcl
    

    相反,您必须将选项 -column 和 -header 与 sqlite 命令一起使用,如下所示:

    $ sqlite3 -column -header <dbfile> "select * from MyTable"
    

    使用:

    $ sqlite3 --version 3.8.11.1 2015-07-29 20:00:57 cf538e2783e468bbc25e7cb2a9ee64d3e0e80b2f
    
    • 18
  4. Geoff_Clapp
    2019-05-08T13:13:47+08:002019-05-08T13:13:47+08:00

    我总是用

    .mode line
    

    它垂直打印查询结果,类似于 MySQL 的\G修饰符。

    • 13
  5. Cho-Lung
    2015-10-20T13:05:55+08:002015-10-20T13:05:55+08:00

    因为我还不能发表评论......除了 Mat 和 mlissner 已经提供的很好的答案之外,如果在任何情况下列的内容被截断,在给 sqlite shell 提供正确的格式之后(使用.mode column和.headers on如上所述),也可以使用.explain这样的方式显示一列的全部内容。

    此命令的唯一缺点是列标题缩小,因此无法正确读取它们并且输出可能非常混乱(在视觉场景中),然后您可以使用.explain off返回到以前的格式并以更“人性化”的方式查看它可读”格式再次。

    这种方法可以与输出格式化程序命令结合使用,并作为查看数据库/列的完整数据的临时解决方案,因为.width您始终必须给出精确的字符数才能获得完整的输出列的数据。

    有关更改输出格式的更多信息,请快速参考默认 CLI 文档:

    https://www.sqlite.org/cli.html

    • 1
  6. Kevin
    2019-08-18T09:06:36+08:002019-08-18T09:06:36+08:00

    您可以.mode tabs使用方便。

    sqlite> select * from user;
    name    age
    Bob     18
    Ali     19
    
    • 1
  7. superheron
    2017-11-13T09:32:21+08:002017-11-13T09:32:21+08:00

    我的看起来很乱,没有换行符。@博轩评论

    您可能还想添加 .separator ROW "\n",以便行由换行符分隔。我的不是,输出不可读。——博轩5月11日15:08

    也解决了我的问题在此处输入图像描述

    • 0
  8. luis_js
    2020-05-11T20:50:51+08:002020-05-11T20:50:51+08:00

    除了已经说过的所有内容之外,您还可以使用 .width 控制显示的列的宽度。

    例子

    .width 8 0 3 9
    

    然后您的输出将显示您的第一列宽度为 8 个字符,第二列自动调整(见下文),第三列宽度为 3 个字符,第四列宽度为 9 个字符。

    替代 .width,使用一个用空格填充的别名。

    优点是您在查询中只为那些需要额外宽度的列动态调整宽度,而其余列继续使用自动调整宽度。

    所有列都将使用基于 sqlite 文档宽度规则的自动调整宽度:

    如果将列的宽度指定为 0,则列宽会自动调整为三个数字中的最大值:10、表头宽度和第一行数据的宽度。这使得列宽可以自我调整。每列的默认宽度设置是这个自动调整的 0 值。

    假设您的表“my_table”具有“名称”、“年龄”和“地址”列。您有兴趣展示:

    1. “名称”:前 20 个字符
    2. “年龄”:自动调整
    3. “地址”:前 30 个字符

    您的查询将是:

    .mode columns
    .headers on
    
    CREATE TABLE my_table (name TEXT, age INTEGER, address TEXT);
    
    INSERT INTO my_table VALUES ("short name",
    22, "my house");
    
    INSERT INTO my_table VALUES ("my name is very long",
    22, "i live in my house somewhere in the planet Earth");
    
    SELECT name AS "name                ",
           age,
           address AS "address                       "
    FROM my_table;
    

    你的输出:

    name                  age         address                       
    --------------------  ----------  ------------------------------
    short name            22          my house                      
    my name is very long  22          i live in my house somewhere i
    
    • 0
  9. e-ruiz
    2022-10-08T13:50:48+08:002022-10-08T13:50:48+08:00

    请注意,SQLite3 有很多.mode选项。让我们来看看:

        sqlite> .version
        SQLite 3.34.1 2021-01-20
    
        sqlite> .help .mode 
        .import FILE TABLE       Import data from FILE into TABLE
           Options:
             --ascii               Use \037 and \036 as column and row separators
             --csv                 Use , and \n as column and row separators
             --skip N              Skip the first N rows of input
             -v                    "Verbose" - increase auxiliary output
           Notes:
             *  If TABLE does not exist, it is created.  The first row of input
                determines the column names.
             *  If neither --csv or --ascii are used, the input mode is derived
                from the ".mode" output mode
             *  If FILE begins with "|" then it is a command that generates the
                input text.
        .mode MODE ?TABLE?       Set output mode
           MODE is one of:
             ascii     Columns/rows delimited by 0x1F and 0x1E
             box       Tables using unicode box-drawing characters
             csv       Comma-separated values
             column    Output in columns.  (See .width)
             html      HTML <table> code
             insert    SQL insert statements for TABLE
             json      Results in a JSON array
             line      One value per line
             list      Values delimited by "|"
             markdown  Markdown table format
             quote     Escape answers as for SQL
             table     ASCII-art table
             tabs      Tab-separated values
             tcl       TCL list elements
    

    对于原来的问题我们一定要看box,table当然column。

    好吧,column需要一些帮助.headers on来显示标题,而box模式table并不关心,它们总是显示标题。

    这两种模式在显示大长度数据时会破坏布局。而且,再次box和table模式不关心.width(这是一个遗憾)。

    当我们需要在 SQLite CLI 中处理大篇幅时,最好.mode使用line. 这样每一列都将逐行显示,实例将用空行分隔。

    旁注:我不确定,但在提出问题时,.mode line SQLite 中不可用。

    另外(但完全偏离主题),探索的好模式,特别是用于数据导出,是:csv,json,insert等等。

    因此,结帐sqlite> .help .mode并进行自己的尝试。

    更新:

    我们还可以通过一个小技巧应用函数来限制列大小substr():

    select 
      substr(my_column_with_lot_chars, 1, 30) as my_column_with_lot_chars, 
      other_column, 
      and_so_on 
    from my_table;
    

    始终检查文档:https ://www.sqlite.org/cli.html#dot_command_execution

    =)

    • 0

相关问题

  • 在 SQLite 上写入一行需要多少次磁盘寻道?

  • SQLite 是否有可用于数据复制的免费软件?[关闭]

  • 在客户端应用程序上使用 CoreData

  • SQLite 的限制

  • 是否可以将 SQLite 用作客户端-服务器数据库?[关闭]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    授予用户对所有表的访问权限

    • 5 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    pedrosanta 使用 psql 列出数据库权限 2011-08-04 11:01:21 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve