开发者

Sending Double typed data directly from C to MySQL

I am using MySQL C API. Now I have some Double Typed data in C, and I want to insert them to a database. All I know now is to use mysql_real_query() with a statement string to do this. I am worrying a开发者_JAVA百科bout losing precision, since the process above involves double to string then to double. Is there a way to avoid this? Like sending double binary data directly to MySQL? I assume MySQL implements double the same as C does.


You should use a prepared statement with a placeholder for your double and then you can bind a double without converting anything to a string. Something like this:

char *insert = "insert into pancakes (d) values (?)";
MYSQL_BIND b;
MYSQL_STMT *stmt = mysql_stmt_init(mysql);
mysql_stmt_prepare(stmt, insert, strlen(insert));

b.buffer_type = MYSQL_TYPE_DOUBLE;
b.buffer = &your_double;
/* Fill in the rest of b...*/
mysql_stmt_bind_param(stmt, &b);

References:

  • mysql_stmt_init
  • mysql_stmt_prepare
  • Type Codes
  • mysql_stmt_bind_param
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜