SELECT prepared statement fails with error 1295 in MariaDB

673 Views Asked by At

I'm trying to run the following query via MariaDB Connector/C using a prepared statement:

SELECT * FROM customers WHERE ApiKey=?

However, mysql_stmt_execute() returns error code 1295:

This command is not supported in the prepared statement protocol yet

Below is the approximate code:

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

There are 1 best solutions below

0
Serge Rogatch On

As the search on the internet doesn't give anything useful so far, I have finally found the culprit by means of exclusion. It's the call to mysql_stmt_attr_set(pStmt, STMT_ATTR_ARRAY_SIZE, &nRows); . Apparently, this call only works for INSERT and UPDATE statements and you must not call it when doing a SELECT.