3

Changing the MySQL query delimiter via the C API

 3 years ago
source link: https://www.codesd.com/item/changing-the-mysql-query-delimiter-via-the-c-api.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Changing the MySQL query delimiter via the C API

advertisements

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..

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

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?

Thanks!


EDIT: 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)


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


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:

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.)

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.

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

Tags mysql

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK