我在从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但不是为我工作。你能帮我一下吗?
发布于 2015-09-30 09:56:40
我很惊讶你的第一个解决方案没有奏效:
mysql -h localhost -uuser -ppsw DB -e "set @x=${x}; source script.sql"分析
source是MySQL命令set @x=${x};是一条SQL语句。我认为可能存在将这两种类型合并为一条语句的问题,因为MySQL --execute=statement, -e statement应该是执行语句并退出。
和退出部分是为什么当我尝试我的第一个想法时重定向的stdin被忽略的原因:
mysql -h localhost -uuser -ppsw DB -e "set @x=${x};" < script.sql解决方案
经过进一步的实验,我发现只需在source命令中添加一个分号就可以防止语法错误。我不能解释为什么通常不需要用分号来终止list的最后一条SQL语句,但这里有:
mysql -h localhost -uuser -ppsw DB -e "set @x=${x}; source script.sql;"正如Glenn所指出的,如果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 |
+----------------------+
| |
+----------------------+https://stackoverflow.com/questions/32823274
复制相似问题