如何让 mysql 命令行客户端不打印 select * 中的 blob 字段

发布于 2024-09-25 07:34:57 字数 162 浏览 4 评论 0原文

探索一些具有 blob 字段的表。我如何使用命令行客户端执行 select * 并让它抑制 blob 字段的打印(或截断为标准字段宽度),而不是在屏幕上滚动一堆二进制垃圾?这是 mysql 5.1 客户端。只是想执行 select * 而不是单独列出所有非 blob 字段以进行开发。

Exploring some tables which have blob fields. How could I do a select * with the command line client and have it surpress the printing (or truncate to a standard field width) the blob fields rather than scrolling a bunch of binary junk on the screen? This is with mysql 5.1 client. Just want to do a select * and not list all of the non-blob fields individually, for development.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈下烟灰 2024-10-02 07:34:57

这可以在 MySQL 中本机执行,但它相当笨重:

SET @sql=CONCAT('SELECT ', (SELECT GROUP_CONCAT(COLUMN_NAME) FROM 
    INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='test' AND TABLE_NAME='test' 
    AND DATA_TYPE!='blob'), ' FROM test.test');
PREPARE preparedsql FROM @sql;
EXECUTE preparedsql;
DEALLOCATE PREPARE preparedsql;

相对于 MySQL 过程,我通常更喜欢 BASH 别名/函数,因为它们在系统之间更易于传输:

function blobless()
{
  cols=''
  _ifs=$IFS
  IFS=$(echo -en "\n\b")
  for col in $(mysql --skip-column-names -e "SELECT COLUMN_NAME FROM 
      INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='$1' AND TABLE_NAME='$2' 
      AND DATA_TYPE NOT LIKE '%blob'"); do 
    cols="$cols,$col"
  done
  IFS=$_ifs
  mysql -e "SELECT $(echo $cols | cut -c2-) FROM $1.$2 $3"
}

像这样调用:

[andy ~]# blobless test test "where id>0"
+----+--------+
| id | t      |
+----+--------+
|  1 | 123    |
|  2 | 124213 |
+----+--------+

如果您在 MySQL 客户端控制台中,请使用 Ctrl- Z 暂停程序并放入 shell。然后使用blobless db table检查该blobless数据。 fg会将挂起的作业(即MySQL客户端)恢复到前台。

您可以在 ~/.my.cnf 中设置默认 MySQL 连接详细信息 (howto) 使您不必在命令行上提供主机/用户/密码 - BASH 也将使用它功能。

This can be performed natively in MySQL, but it's quite unwieldy:

SET @sql=CONCAT('SELECT ', (SELECT GROUP_CONCAT(COLUMN_NAME) FROM 
    INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='test' AND TABLE_NAME='test' 
    AND DATA_TYPE!='blob'), ' FROM test.test');
PREPARE preparedsql FROM @sql;
EXECUTE preparedsql;
DEALLOCATE PREPARE preparedsql;

I generally prefer BASH aliases/function to MySQL procedures as they're more transportable between systems:

function blobless()
{
  cols=''
  _ifs=$IFS
  IFS=$(echo -en "\n\b")
  for col in $(mysql --skip-column-names -e "SELECT COLUMN_NAME FROM 
      INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='$1' AND TABLE_NAME='$2' 
      AND DATA_TYPE NOT LIKE '%blob'"); do 
    cols="$cols,$col"
  done
  IFS=$_ifs
  mysql -e "SELECT $(echo $cols | cut -c2-) FROM $1.$2 $3"
}

Invoke like so:

[andy ~]# blobless test test "where id>0"
+----+--------+
| id | t      |
+----+--------+
|  1 | 123    |
|  2 | 124213 |
+----+--------+

If you are in the MySQL client console use Ctrl-Z to suspend the program and drop to the shell. Then use blobless db table to check that blobless data. fg will restore the suspended job (i.e. MySQL client) to the foreground.

You can set default MySQL connection details in ~/.my.cnf (howto) to save you having to supply host/user/pass on the command line - this will also be used by BASH functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文