How to use parameters using MySQL and a TADOQuery instance?

  ado, c++, c++builder, mysql, odbc

I have a TADOConnection pointing to a MySQL 8.0 instance. The connection is tested and it works. Following this example on how to use prepared statement, I’m having an error and I have no idea why.

This code works fine, it will return true from the very last statement. No errors, no warnings.

AnsiString sqlQuery = "SELECT e.name FROM employee e WHERE e.id = 1;";

if (!_query->Connection->Connected) {
  try {
    _query->Connection->Connected = true;
  } catch (EADOError& e) {
    return false;
  }
}

_query->SQL->Clear();
_query->SQL->Add(sqlQuery);

_query->Prepared = true;

try {
  _query->Active = true;

  if (_query->RecordCount == 0) {
    return false;
  }
} catch (EADOError& e) {
  return false;
}

return true;

However, this code fails executing _query->SQL->Add(sqlQuery); with this error:

Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.

AnsiString sqlQuery = "SELECT e.name FROM employee e WHERE e.id = :id;";

if (!_query->Connection->Connected) {
  try {
    _query->Connection->Connected = true;
  } catch (EADOError& e) {
    return false;
  }
}

_query->SQL->Clear();
_query->SQL->Add(sqlQuery);     // <---- error here

_query->Parameters->ParamByName("id")->Value = id;

_query->Prepared = true;

try {
  _query->Active = true;

  if (_query->RecordCount == 0) {
    return false;
  }
} catch (EADOError& e) {
  return false;
}

return true;

Everywhere I find examples, all of them use :paramName to specify parameters. What am I missing?

Source: Windows Questions C++

LEAVE A COMMENT