我正在尝试使用PHP创建存储过程。我的阅读表明通过在PHP中使用'exec'命令运行.sql文件来实现此目的的最佳方法。
在测试中,我创建了一个名为amtv3_create_routines.sql的文件,其中包含以下内容:
DELIMITER //
DROP PROCEDURE IF EXISTS createTc //
CREATE PROCEDURE createTc()
BEGIN
drop table if exists v3_tc;
CREATE TABLE v3_tc (
source BIGINT UNSIGNED NOT NULL ,
dest BIGINT UNSIGNED NOT NULL ,
PRIMARY KEY (source, dest) )
ENGINE = InnoDB;
insert into v3_tc (source, dest)
select distinct rel.sourceid, rel.destinationid
from rf2_ss_relationships rel inner join rf2_ss_concepts con
on rel.sourceid = con.id and con.active = 1
where rel.typeid = (select distinct conceptid from rf2_ss_descriptions where term = 'is a')
and rel.active = 1;
REPEAT
insert into v3_tc (source, dest)
select distinct b.source, a.dest
from v3_tc a
join v3_tc b on a.source = b.dest
left join v3_tc c on c.source = b.source and c.dest = a.dest
where c.source is null;
set @x = row_count();
select concat('Inserted ', @x);
UNTIL @x = 0 END REPEAT;
create index idx_v3_tc_source on v3_tc (source);
create index idx_v3_tc_dest on v3_tc (dest);
END //
DELIMITER;
当我手动将其输入mysql 5.6.22时,此代码正常工作 但是,如果我保存文件并从提示符输入命令。
mysql -uroot -p -hlocalhost amtv3 < [full path]/amtv3_create_routines.sql
我尝试使用utf8编码和Windows 1252编码保存文件。
在命令提示符下,没有反馈,并且未创建过程。
在PHP中,我使用的是codeigniter框架。如果我使用db->query
方法,我可以创建存储过程,但数据库失去连接。发布$db->reconnect()
有效,但不可靠。
有关如何创建存储过程的任何建议吗?
答案 0 :(得分:0)
省略最后一行中的空格DELIMITER;
会导致语法错误(可能还有其他原因导致未打印此错误)。
答案 1 :(得分:0)
您可以尝试
mysql -h localhost -U <username> -d <database_name> -f /home/user/<procedure_name>.sql