我正在尝试使用准备好的语句通过 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);
。显然,此调用仅适用于 INSERT
和UPDATE
语句,并且在执行 SELECT
时不得调用它.
关于c++ - MariaDB 中的 SELECT 准备语句失败并出现错误 1295,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61806280/