Error when using user defined variables in MySQL query via libmysqlclient

Iceman :

The query SET @t=NOW(); INSERT INTO tests(posted) VALUES(@t); from C++ code (libmysqlclient) results in the following message:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO tests(posted) VALUES(@t)' at line 1

But the query works fine from console or HeidiSQL.

Table "tests":
'id' int(10) unsigned NOT NULL AUTO_INCREMENT, 'posted' datetime NOT NULL, PRIMARY KEY ('id')

main.cpp

#include <cstdio>
#include "sqldb.h"

int main(int argc, char** argv) {
    MySQLClient DB;

    if (!DB.Connect("192.168.1.254", "test", "testpass")) {
        printf("MySQL: %s\n", DB.Error());
        return 1;
    }
    if (!DB.UseDB("test")) {
        printf("MySQL: %s\n", DB.Error());
        return 2;
    }
    if (!DB.Query("SET @t=NOW(); INSERT INTO tests(posted) VALUES(@t);")) {
        printf("MySQL: %s\n", DB.Error());
        return 3;
    }
    return 0;
}

Function "Query"

bool MySQLClient::Query(const char * statement) {
    if (!ctx || !statement) return false;
    unsigned long length = 0;
    while(statement[length]) ++length;
    return !mysql_real_query(static_cast<MYSQL*>(ctx), statement, length);
}

Why `libmysqlclient can't process this query?

rua.kr :

CLIENT_MULTI_STATEMENTS enables mysql_query() and mysql_real_query() to execute statement strings containing multiple statements separated by semicolons.

    mysql_real_connect(mysql, server, username, password, db, 0, NULL, CLIENT_MULTI_STATEMENTS);

multiple queries with mysql_query in a c++ project

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=276596&siteId=1