在Bash脚本文件中写入存储过程

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

我正在尝试在bash脚本中执行存储过程。这是脚本文件

#!/bin/bash
mysql -u $1 -p$2  -e " 
create database test;
use androidcrashes;
DELIMITER $$; 
CREATE PROCEDURE test.createTables ()
BEGIN
    CREATE TABLE Employee (
    id INT PRIMARY KEY,
    Name varchar(100),
    Department varchar(200) NOT NULL
    );
END$$
"

这里$ 1和$ 2分别是我作为命令行参数传递的数据库的用户和密码。当我尝试在MySQL控制台中运行相同的命令时,它工作正常,但是当我运行脚本文件时,出现以下错误

ERROR 1064 (42000) at line 5: 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 '' at line 8

有人可以指出我要去哪里了吗?

mysql bash shell stored-procedures
1个回答
1
投票
也请在字符串周围使用单引号。

#!/bin/bash mysql -u $1 -p$2 -e ' create database test; use androidcrashes; DELIMITER $$ CREATE PROCEDURE test.createTables () BEGIN CREATE TABLE Employee ( id INT PRIMARY KEY, Name varchar(100), Department varchar(200) NOT NULL ); END$$ '

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