2

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);
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
  • 1
    When I search the Internet for that error, I usually see it in regard to statements like `USE` and `LOAD DATA`. I'm not sure why it would happen with `SELECT`. – Barmar May 14 '20 at 20:04
  • 1
    Don't forget that MySQL has a [C++ Connector](https://dev.mysql.com/doc/connector-cpp/8.0/en/) that's *significantly* more pleasant to use than the C one. – tadman May 14 '20 at 20:04

1 Answers1

3

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.

Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158