通过 C API 更改 MySQL 查询分隔符 [英] Changing the MySQL query delimiter through the C API

查看:54
本文介绍了通过 C API 更改 MySQL 查询分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 C API 更改 MySQL 查询分隔符?我尝试发送 DELIMITER | 作为查询,抱怨 ..在第 1 行的分隔符"附近使用正确的语法..

How can I change the MySQL query delimiter using the C API? I tried sending DELIMITER | as a query, complained about ..the right syntax to use near 'delimiter' at line 1..

也试过 DELIMITER |; ,没有运气.试过 DELIMITER |;选择 1|,没有运气.:(

Tried DELIMITER |; too, no luck. Tried DELIMITER |; SELECT 1|, no luck. :(

我问的原因是我需要通过 C API 创建一个触发器,如下所示:

The reason I ask is that I need to create a trigger through the C API such as the following:

create trigger increase_count after insert on torrent_clients for each row
begin
   IF NEW.BytesLeft = 0 THEN
      UPDATE torrents SET Seeders = Seeders + 1 WHERE torrents.InfoHash = NEW.InfoHash;
   ELSE
      UPDATE torrents SET Leechers = Leechers + 1 WHERE torrents.InfoHash = NEW.InfoHash;
   END IF;
end|

但我认为没有办法在不更改分隔符的情况下做到这一点,是吗?

but I don't think there is a way to do it without changing the delimiter, is there?

谢谢!

请注意,我确实需要在最后明确写出分隔符,因为我只使用一个 API 调用运行多个查询(我有多个语句)

Note that I do need to explicitly write the delimiter at the end, as I'm running multiple queries with only one API call (I have multi statements on)

使用 C api 的 mysqlclient 执行此操作,因此必须有一种方法..

the mysqlclient that uses the C api does this so there must be a way..

推荐答案

仅在使用 mysql 客户端程序时才需要更改分隔符(因为它是 mysql 将分号解释为语句分隔符).您不需要更改分隔符使用 C API 时:

Changing the delimiter is only needed when using the mysql client program (because it is mysql that interpretes the semicolon as statement delimiter). You don't need to change the delimiter when using the C API:

通常,您只能使用 <执行单个 SQL 命令代码>mysql_query().只有在语法允许的情况下,分号才能出现在这样的命令中,而通常情况并非如此!特别是,SQL 不允许命令以分号结尾.(如果您尝试这样做,C API 将引发错误.)

Normally, you can execute only a single SQL command with mysql_query(). A semicolon can appear in such a command only when it is syntactically allowed, and that is normally not the case! In particular, SQL does not allow for a command to end in a semicolon. (The C API will raise an error if you attempt to do so.)

CREATE PROCEDURECREATE FUNCTIONCREATE TRIGGER 等命令在定义存储过程或触发器时是例外:此类命令分号用作 SQL 指令之间的分隔符,这些 SQL 指令是存储过程或触发器的一部分.这样的命令可以毫无问题地执行.

The commands CREATE PROCEDURE, CREATE FUNCTION, CREATE TRIGGER, and the like are exceptions when they define a stored procedure or trigger: In such commands the semicolon serves as a separator between the SQL instructions that are part of the stored procedure or trigger. Such commands can be executed without problem.

附言您可能应该关闭 multi statements再次.

P.S. You probably should set multi statements off again.

这篇关于通过 C API 更改 MySQL 查询分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆