MariaDB 在 phpmyadmin 中工作,但不能在 bash 中工作

问题描述 投票:0回答:1

这一行:

use test; SELECT `xyflagged`.`Article number`, `xyflagged`.`Flag` INTO outfile 'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' FROM xyflagged;

但是,这有效:

"use test; SELECT * INTO outfile 'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' FROM xyflagged;

我已经研究了 SO 上的几个解决方案,例如 thisthis 以及更多关于转义字符串的解决方案。显然,它与列名中的空格有关(我无法控制)。所以我尝试了各种组合:

use test; SELECT `xyflagged`.`Article number`, `xyflagged`.`Flag` INTO outfile 'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' FROM xyflagged;

use test; SELECT `Article number`, `Flag` INTO outfile 'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' FROM xyflagged;

use test; SELECT [xyflagged].[Article number], [xyflagged].[Flag] INTO outfile 'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' FROM xyflagged;

正如各种答案和括号所建议的那样,它似乎对某些人有效,但对我无效。

我试过这样:

mysql -u me -paaaaaaaaaaaaaaa -e "use test; SELECT `xyflagged`.`Article number`, `xyflagged`.`Flag` INTO outfile 'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' FROM xyflagged";

还有这样的:

mysql -u me -paaaaaaaaaaaaaaa << EOF
use test; SELECT `xyflagged`.`Article number`, `xyflagged`.`Flag` INTO outfile 'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' FROM xyflagged;
EOF

错误总是:

ERROR 1064 (42000) at line 1:您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,了解在第 1 行的“INTO outfile 'Test1618' fields returned by ...”附近使用的正确语法

也许这是基本或明显的东西,但我想我已经尝试了所有组合,但无法使其发挥作用。

bash mariadb
1个回答
1
投票

Bash 将对任何双引号字符串执行插值。 反引号表示在此上下文中的命令替换。因此以下内容不起作用,因为 bash 尝试运行

xyflaged
并将其替换为命令的输出。

mysql -u me -paaaaaaaaaaaaaaa -e "use test; SELECT
`xyflagged`.`Article number`, `xyflagged`.`Flag` INTO outfile
'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated
by '\n' FROM xyflagged";

单引号字符串不受插值的影响。但是,单引号在 bash 中也很奇怪,因为它们禁用所有特殊字符。这意味着您不能在单引号字符串中使用反斜杠转义单引号,因为反斜杠并不特殊;

'\''
不是有效的 bash 表达式。

他的下一个被称为“heredoc”。它有同样的字符串插值问题。

mysql -u me -paaaaaaaaaaaaaaa << EOF
use test; SELECT `xyflagged`.`Article number`, `xyflagged`.`Flag` INTO outfile 'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' FROM xyflagged;
EOF

但是,如果您将heredoc文件结束符(此处为

EOF
)放在单引号内,它将禁用heredoc中的特殊字符。这效果更好,因为由于您没有用单引号引用搅拌,所以它们不会 对 bash 有特殊意义:

mysql -u me -paaaaaaaaaaaaaaa << 'EOF'
use test; SELECT `xyflagged`.`Article number`, `xyflagged`.`Flag` INTO outfile 'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' FROM xyflagged;
EOF

或者,您可以在文件中提供输入(名称/扩展名无关),bash 会将该文件的内容提供给程序的标准输入:

mysql -u me -paaaaaaaaaaaaaaa < my-commands.sql

我更喜欢最后一个调用,因为如果你给它一个合理的扩展名(如本例中的

.sql
),大多数编辑器都足够聪明,可以在文件上进行语法突出显示,这使得编辑更容易。

© www.soinside.com 2019 - 2024. All rights reserved.