C++ XDevAPI multiple .values

360 views Asked by At

I am using Xcode, MySQL and XDevAPI.

I have the following table on the database

create table TABLE(ID INT, VALUE_01 INT, VALUE_02 INT, VALUE_03 INT);

I have the following values on code the code:

Table table(session, "TABLE");
int id;
set<int> numbers;

id = 2395;
numbers.insert(1);
numbers.insert(2);
numbers.insert(3);

So I wanted to try:

table.insert("ID", "VALUE_01", "VALUE_02", "VALUE_03")
  .values(id)
  .values(numbers)
  .execute();

However, I am getting a runtime error saying:

libc++abi.dylib: terminating with uncaught exception of type mysqlx::abi2::r0::Error: CDK Error: Wrong number of fields in row being inserted

Can you help me, please?

1

There are 1 answers

5
Asteroids With Wings On BEST ANSWER

You have two problems here.

First, you're invoking values twice instead of once, and both times you're doing it with the wrong [number of] arguments. You're supposed to provide values() once per row, each time providing a value for each field in that row (ref).

Second, a std::set<int> is ordered, meaning that for different inputs than 1, 2, & 3 your values could be stored in a different order to what you intended.

I suggest a std::vector<int> instead.

Table table(session, "TABLE");
int id;
vector<int> numbers;

id = 2395;
numbers.push_back(1);
numbers.push_back(2);
numbers.push_back(3);

table.insert("ID", "VALUE_01", "VALUE_02", "VALUE_03")
  .values(id, numbers[0], numbers[1], numbers[2])
  .execute();