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?

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

5 Comments

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? dev.mysql.com/doc/x-devapi-userguide/en/…
@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.
@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.
The annoying thing is that I CANNOT use .values(id, numbers)! :) I think I will have to add another variadic version of .values()! :)
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.