在通过libmysqlclient的MySQL查询中使用用户定义的变量时出错

问题描述 投票:0回答:1

来自C ++代码(libmysqlclient)的查询SET @t=NOW(); INSERT INTO tests(posted) VALUES(@t);导致以下消息:

您的SQL语法有误;检查手册对应于您的MariaDB服务器版本以使用正确的语法第1行“ INSERT INTO测试(张贴)VALUES(@t)”附近

但是查询可以从控制台或HeidiSQL正常工作。

表“测试”:'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;
}

功能“查询”

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);
}

为什么`libmysqlclient无法处理该查询?

c++ mysql
1个回答
1
投票

CLIENT_MULTI_STATEMENTS使mysql_query()和mysql_real_query()能够执行包含由分号分隔的多个语句的语句字符串。

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

multiple queries with mysql_query in a c++ project

© www.soinside.com 2019 - 2024. All rights reserved.