0

I'm using Mariadb C api. Can I have an example of binding BIT type data? I can't find any of such examples on the Internet.

Here is part of my code

char** test_data = new char*[2];

test_data[0] = new char{'0'};

test_data[1] = new char{'1'};

MYSQL_BIND *bind = new MYSQL_BIND;

bind->buffer_type = MYSQL_TYPE_BIT;

bind->buffer = test_data;

mysql_stmt_attr_set(insertStmt, STMT_ATTR_ARRAY_SIZE, &numRows);

mysql_stmt_bind_param(insertStmt, bind);

mysql_stmt_execute(insertStmt);

The program crashes when executing the code.

I also tried

char** test_data = new char*[2];

test_data[0] = new char{'0'};

test_data[1] = new char{'1'};

MYSQL_BIND *bind = new MYSQL_BIND;

bind->buffer_type = MYSQL_TYPE_STRING;

bind->buffer = test_data;

mysql_stmt_attr_set(insertStmt, STMT_ATTR_ARRAY_SIZE, &numRows);
mysql_stmt_bind_param(insertStmt, bind);

mysql_stmt_execute(insertStmt);

And there is no data fetched back when doing so.

Marco
  • 7,007
  • 2
  • 19
  • 49
popcornhex
  • 21
  • 1
  • This is C++ and not C. – Marco Aug 10 '23 at 20:02
  • And please format your code properly! – Marco Aug 10 '23 at 20:03
  • I think the root of your issue is because you're mixing C++ concepts (like `new`) with C. – Marco Aug 10 '23 at 20:06
  • I think you should find the documentation what C++ type is mapped to `MYSQL_TYPE_BIT` or to `MYSQL_TYPE_STRING`. The way you allocate the memory looks suspicious – Dmytro Ovdiienko Aug 10 '23 at 21:14
  • I use MariaDB c api in my C++ code. It works fine for all the other data types, except for MYSQL_TYPE_BIT. I don't think c vs c++ is the cause here. – popcornhex Aug 11 '23 at 19:56
  • @DmytroOvdiienko How would you write this in c? I would like an example that works. – popcornhex Aug 12 '23 at 10:19
  • @popcornhex I do not have the dev environment to create the sample code for you. At first ensure you really need to allocate the `MYSQL_BIND` on the heap. In the documentation this structure is allocated on the stack as array. Otherwise you need to `delete` after the SQL statement is executed. On MYSQL_TYPE_BIT see the manual: https://dev.mysql.com/doc/c-api/8.0/en/c-api-prepared-statement-type-codes.html. Also check the MYSQL source code. It seems you are missing `buffer_length` initialization. – Dmytro Ovdiienko Aug 16 '23 at 12:43
  • @popcornhex For example of how to use the MYSQL_BIND instruction, see the unit test: https://github.com/google/mysql/blob/master/tests/mysql_client_test.c#L617 – Dmytro Ovdiienko Aug 16 '23 at 13:03

0 Answers0