0

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.