gpt4 book ai didi

mysql - shell 脚本 : Launch Mysql script using shell variable

转载 作者:行者123 更新时间:2023-11-29 02:53:47 25 4
gpt4 key购买 nike

我在从 shell 启动 MySQL 脚本时遇到问题。我使用文件名为变量 ${x} 赋值。所以我必须使用这个变量启动一个 MySQL 脚本。我想启动脚本而不在 shell 中插入所有 MySQL 代码(太长)但使用:

mysql -h localhost -uuser -ppsw DB < script.sql

我的设想是:

mysql -h localhost -uuser -ppsw DB -e "set @x=${x}; source script.sql"

mysql -h localhost -uuser -ppsw DB -e "set @x=${x};"
mysql -h localhost -uuser -ppsw DB< script.sql

但对我不起作用。你能帮帮我吗?

最佳答案

我很惊讶你的第一个解决方案没有奏效:

mysql -h localhost -uuser -ppsw DB -e "set @x=${x}; source script.sql"

分析

  • source 是一个 MySQL command
  • set @x=${x};是一条SQL语句。

我认为将这两种类型组合为一条语句可能存在问题,因为 MySQL --execute=statement, -e statement 应该是 execute the statement并退出。

and quit 部分是为什么当我尝试我的第一个想法时重定向的标准输入被忽略:

mysql -h localhost -uuser -ppsw DB -e "set @x=${x};" < script.sql

解决方案

经过进一步的实验,我发现只需在 source 命令后附加一个分号就可以防止出现语法错误。我不能说为什么这会起作用,因为通常不需要分号来终止列表的最后一个 SQL 语句,但你已经知道了:

mysql -h localhost -uuser -ppsw DB -e "set @x=${x}; source script.sql;"

正如 Glenn Jackman 所指出的,如果 shell 变量是一个非数字字符串,则 shell 变量必须用单引号引起来,以便在分配 MySQL 变量时,MySQL 将处理右侧( shell 变量)作为字符串文字而不是作为列名的标识符:

mysql -h localhost -uuser -ppsw DB -e "set @x='$x'; source script.sql;"

这个版本也可以安全地处理数字字符串,如下例所示。我还删除了 shell 变量周围的花括号,因为它们不是必需的。

例子

t.sql 的内容:

select now();
select @variable as 'Contents of variable';

使用数字字符串作为 shell 变量:

$ number=3
$ mysql -e "set @variable=$number; source t.sql;"
+---------------------+
| now() |
+---------------------+
| 2015-10-02 13:06:45 |
+---------------------+
+----------------------+
| Contents of variable |
+----------------------+
| 3 |
+----------------------+

使用非数字字符串作为 shell 变量会产生错误:

$ text=text
$ mysql -e "set @variable=$text; source t.sql;"
ERROR 1054 (42S22) at line 1: Unknown column 'text' in 'field list'

$ text="This is a string"
$ mysql -e "set @variable=$text; source t.sql;"
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a string' at line 1

现在将 shell 变量用单引号括起来:

$ mysql -e "set @variable='$text'; source t.sql;"
+---------------------+
| now() |
+---------------------+
| 2015-10-02 13:08:04 |
+---------------------+
+----------------------+
| Contents of variable |
+----------------------+
| This is a string |
+----------------------+

$ text=text
$ mysql -e "set @variable='$text'; source t.sql;"
+---------------------+
| now() |
+---------------------+
| 2015-10-02 13:10:53 |
+---------------------+
+----------------------+
| Contents of variable |
+----------------------+
| text |
+----------------------+

$ mysql -e "set @variable='$number'; source t.sql;"
+---------------------+
| now() |
+---------------------+
| 2015-10-02 13:11:42 |
+---------------------+
+----------------------+
| Contents of variable |
+----------------------+
| 3 |
+----------------------+

使用不存在的 shell 变量会将 MySQL 变量设置为空字符串:

$ mysql -e "set @variable='$nonexistent'; source t.sql;"
+---------------------+
| now() |
+---------------------+
| 2015-10-02 13:06:14 |
+---------------------+
+----------------------+
| Contents of variable |
+----------------------+
| |
+----------------------+

关于mysql - shell 脚本 : Launch Mysql script using shell variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32823274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com