c++ - MariaDB 中的 SELECT 准备语句失败并出现错误 1295

标签 c++ sql mariadb prepared-statement mariadb-connector-c

我正在尝试使用准备好的语句通过 MariaDB Connector/C 运行以下查询:

SELECT * FROM customers WHERE ApiKey=?

但是,mysql_stmt_execute()返回错误代码1295:

This command is not supported in the prepared statement protocol yet

下面是大概的代码:

MYSQL mysql;
// ... Initialization of the connection
string query = "SELECT * FROM customers WHERE ApiKey=?";
MYSQL_STMT* pStmt = mysql_stmt_init(&mysql);
mysql_stmt_prepare(pStmt, query.c_str(), query.size());
constexpr const unsigned int nRows = 1;
unsigned long apiKeyLen[nRows] = { unsigned long(apiKey.size()) };
const char* pApiKey[nRows] = { apiKey.c_str() };
MYSQL_BIND bind[nParams];
memset(bind, 0, sizeof(bind));
bind[0].buffer_type = MYSQL_TYPE_STRING;
bind[0].buffer = pApiKey;
bind[0].length = apiKeyLen;
mysql_stmt_attr_set(pStmt, STMT_ATTR_ARRAY_SIZE, &nRows);
mysql_stmt_bind_param(pStmt, pBind);
mysql_stmt_execute(pStmt);

最佳答案

由于网上搜索至今没有任何有用的信息,我终于通过排除的方式找到了罪魁祸首。调用mysql_stmt_attr_set(pStmt, STMT_ATTR_ARRAY_SIZE, &nRows); 。显然,此调用仅适用于 INSERTUPDATE语句,并且在执行 SELECT 时不得调用它.

关于c++ - MariaDB 中的 SELECT 准备语句失败并出现错误 1295,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61806280/

相关文章:

sql - 为什么要使用相关子查询?

mysql - 有没有办法限制表中返回唯一值的次数?

python - 按下随机键时 cv2.waitKey(0) 不等待 - OpenCV 3.1.0、Python3、Ubuntu

php - 使用循环选择数据并在div MySql php中显示

c++ - 将参数包解压到字符串 View 中

MySQL - 如何按 TOP 中的特定值对 Select 进行排序

PHP 循环 - SQL 性能不佳

php - 有没有办法更改我的主机名来修复 "php_network_getaddresses: getaddrinfo failed: No such host is known."?

C++ 初始化列表重载消歧

c++ - 包含多行函数签名的 Vim foldexpr