1

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?

Nuno
  • 117
  • 4

1 Answers1

0

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();
Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
  • Looking at the diagrams for the CRUD functions, ```.values()``` should just ADD columns, and you can call it several times in the same insert, right? https://dev.mysql.com/doc/x-devapi-userguide/en/crud-ebnf-table-crud-functions.html – Nuno Dec 12 '20 at 23:55
  • @Nuno You can call it several times, yes.... to add several _rows_. Looking at the syntax tells us nothing about the semantics. Unfortunately, the documentation for X DevAPI is lacking, to say the least, but we can tell from the reference page I linked what is supposed to happen. This also mirrors, very closely, how table insertions are done in SQL, which I'm sure is not an accident. – Asteroids With Wings Dec 13 '20 at 00:00
  • @Nuno Think about why they would allow you to add a field value either by putting it in an existing `values()` call, or by adding an extra `values()` call. What would such a redundant design achieve? Whenever the documentation seems vague, we can think to such things to make better guesses. – Asteroids With Wings Dec 13 '20 at 00:01
  • The annoying thing is that I CANNOT use ```.values(id, numbers)```! :) I think I will have to add another variadic version of ```.values()```! :) – Nuno Dec 13 '20 at 00:08
  • @Nuno Yeah I can see how that's a bit annoying. Even if you didn't have an ID field, `.values(numbers)` wouldn't have been right, as that's for containers of `Row` objects, not of arguments for a single row. Meh. – Asteroids With Wings Dec 13 '20 at 00:13