I suppose that I work with these MySQL control structures (see below) in absolutely wrong way. The problem is maybe in MySQL query, but I am not sure…
MySQL Error: 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 ‘IF @records > 1000 THEN SELECT @actRecords:=COUNT(id) FROM
sys_proc_errWHERE ‘ at line 1
#include <mysql.h>
// other includes, declarations for mysql... etc.
int main() {
mysql_init(&mysql);
mysql_real_connect(&mysql,DB_SERVER,DB_USER,DB_PASSWORD,DB_DB_IN,0,0,CLIENT_MULTI_STATEMENTS);
// Now: DELETE ERROR RECORDS older than 3 days if there is more than 1 000 records, leave 1 000 newest records
mysql_query(connection, "SELECT @records:=COUNT(`id`) FROM `sys_proc_err`;
IF @records > 1000 THEN SELECT @actRecords:=COUNT(`id`) FROM `sys_proc_err` WHERE time > (NOW()-259200);
END IF;
IF @actRecords > 1000 THEN DELETE FROM `sys_proc_err` WHERE `time` < (NOW()-259200);
ELSE DELETE FROM `sys_proc_err` ORDER BY `id` ASC LIMIT @actRecords-1,@records-@actRecords;
END IF;");
return 0;
}
Result for SELECT @records:=COUNT(id) FROM sys_proc_err is 1267.
Result for SELECT @actRecords:=COUNT(id) FROM sys_proc_err WHERE time > (NOW()-259200) is 6.
Checkout multi-statements and multi-queries, multi-results connection options to be passed to mysql_real_connect.
Docs are here http://dev.mysql.com/doc/refman/5.5/en/mysql-options.html