[英]Creating stored procedure on MySQL
I'm trying to run the query below, that is stored in a .sql
file, then read with ioutils.ReadFile and executed on initialization我正在尝试运行下面的查询,该查询存储在.sql
文件中,然后使用ioutils.ReadFile读取并在初始化时执行
CREATE TABLE IF NOT EXISTS districts
(
GeoCode integer PRIMARY KEY,
name varchar(32)
);
drop procedure if exists insert_district;
DELIMITER $$
CREATE PROCEDURE insert_district(in pgeocode int, in pname varchar(32))
BEGIN
INSERT INTO districts(geocode, name) VALUES (pgeocode, pname);
SELECT * FROM districts where geocode = pgeocode;
END$$
DELIMITER ;
I am using the database/sql package and run the query with Exec我正在使用 database/sql 包并使用Exec运行查询
Error 1064: 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 'drop procedure if exists insert_district;
DELIMITER $$
CREATE PROCEDURE insert' at line 7
To the best of my knowledge my syntax is correct, and I tested it so I cannot figure out why the same exact query cannot be run properly from the program.据我所知,我的语法是正确的,我对其进行了测试,所以我无法弄清楚为什么不能从程序中正确运行相同的查询。
The Go MySQL client does not support multiple SQL statements by default. Go MySQL 客户端默认不支持多条 SQL 语句。 You can't just feed it a text file with ;
你不能只给它一个文本文件;
separated statements.分开的陈述。
See Does a Go Mysql driver exist that supports multiple statements within a single string?请参阅是否存在支持单个字符串中的多个语句的 Go Mysql 驱动程序? for details — there's an option you can use to allow multi-statements.有关详细信息 - 您可以使用一个选项来允许多语句。
But that still won't support statements like DELIMITER
which are not recognized by the MySQL Server.但这仍然不支持像DELIMITER
这样不被 MySQL 服务器识别的语句。 That's a mysql client command .那是一个mysql 客户端命令。
You have two alternatives:您有两种选择:
mysql
client in a sub-process using the .sql file as input.使用 .sql 文件作为输入在子进程中执行mysql
客户端。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.