0

I am trying to store a c++ byte array in a mySQL binary(16) field.

mysqlTable is an instance of mysqlx::Table class that works OK with other variable types.

data -> is the table field name of type binary(16)

data1 -> is the table field name of type binary(1)

What I have tried so far:

unsigned char cArray[16];

mysqlTable.insert("data").values(cArray).execute();

I have also tried:

unsigned char * pcArray = new unsigned char[16];

mysqlTable.insert("data").values(pcArray).execute();

And this works just fine:

unsigned char cOneChar;

mysqlTable.insert("data1").values(cOneChar).execute();

The error I get is form the compiler:

see reference to function template instantiation 'mysqlx::TableInsert &mysqlx::TableInsert::values<BYTE*>(BYTE *)' being compiled
    error C2783:    'void mysqlx::internal::Args_processor<mysqlx::internal::Table_insert_detail::Add_value,mysqlx::internal::Table_insert_detail::Add_value::Impl *>::process_args(D,C)': could not deduce template argument for '__formal'
                     with
            [
                D=mysqlx::internal::Table_insert_detail::Add_value::Impl *
            ]

Does anyone know how to do this?

4
  • cArray doesn't carry any information about how big it is, at the very least you'd need to pass a size parameter as well. Check the documentation. Commented Nov 22, 2019 at 1:44
  • 1
    This basically has nothing to do with winapi tag. Commented Nov 22, 2019 at 5:29
  • @JonathanPotter you're right about that. In the case of strings you send a long pointer to a null terminated string, no information about the size. If the string is longer than the field it is truncated. Same case for other types of variables. If they are smaller they are padded and if they are larger they are truncated. There is no prototype of the function that lets you specify the size of the variable. I did check the documentation but it is scarce at best. No information related to this issue. Commented Nov 25, 2019 at 16:01
  • @oWWo Thank you for the remark. The app is being developed in winapi but as you mentioned, the question itself has nothing to do with it. I have removed the winapi tag. Commented Nov 25, 2019 at 16:02

1 Answer 1

1

Late to the party but I struggled to find a neat way to do this. Using msqlx::bytes worked well. So you could adapt your code:

int n=16;
unsigned char cArray[n];
unsigned char const *Ptr=cArray;
mysqlx::bytes bytes{Ptr,n};
mysqlTable.insert("data").values(bytes).execute();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer! It's still relevant since the code is a work in progress and we had to change the variable type but this is the right way to do it. Hope it helps someone else!

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.