C prepared Statements sending bad data
Posted by:
Philip Top
Date: October 17, 2005 11:25PM
I am having a problem with the C api with prepared statements, the data recorded in the database does not match the data I am sending. It seems to be some sort of bit shifted version of the data, and I have no idea why.
I am including code I have been using to test this, followed by the output in the database.
I am using mysql version 4.1.12,
I have tried this on multiple servers, remote and local.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
#define Q1 "insert into test2 (test) values (?)"
int main ()
{
MYSQL *mysql;
/*static MYSQL_FIELD *fields;
*/
MYSQL_STMT *s1;
MYSQL_BIND b1[1];
long testint=0;
int ii;
mysql=mysql_init(NULL);
/*mysql_options(mysql,MYSQL_READ_DEFAULT_GROUP,"mtest");*/
if (!mysql_real_connect(mysql, "", "", "" ,"test1",0,NULL,0))
{
fprintf(stderr, "Failed to connect to Database: Error: %s\n",mysql_error(mysql));
exit(-1);
}
s1=mysql_stmt_init(mysql);
if (!s1)
{
fprintf(stderr, " mysql_stmt_init(), out of memory\n");
exit(-1);
}
memset(b1, 0, sizeof(b1));
b1[0].buffer_type=MYSQL_TYPE_LONG;
b1[0].buffer=(void *) &testint;
if (mysql_stmt_prepare(s1, Q1, strlen(Q1)))
{
fprintf(stderr, " mysql_stmt_prepare(), 1 INSERT failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(s1));
exit(0);
}
if (mysql_stmt_bind_param(s1, b1))
{
fprintf(stderr, " mysql_stmt_bind_param() failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(s1));
exit(0);
}
for (ii=0;ii<20;ii++)
{
testint=ii;
if (mysql_stmt_execute(s1))
{
fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(s1));
exit(0);
}
}
mysql_stmt_close(s1);
mysql_close(mysql);
return 0;
}
mysql> select * from test2;
+------+---------+
| id | test |
+------+---------+
| 1450 | 196864 |
| 1451 | 65536 |
| 1452 | 131072 |
| 1453 | 196608 |
| 1454 | 262144 |
| 1455 | 327680 |
| 1456 | 393216 |
| 1457 | 458752 |
| 1458 | 524288 |
| 1459 | 589824 |
| 1460 | 655360 |
| 1461 | 720896 |
| 1462 | 786432 |
| 1463 | 851968 |
| 1464 | 917504 |
| 1465 | 983040 |
| 1466 | 1048576 |
| 1467 | 1114112 |
| 1468 | 1179648 |
| 1469 | 1245184 |
+------+---------+
20 rows in set (0.06 sec)
the id is just an identifier, the numbers that are supposed to be in the test column are 0-19.
the numbers that are in the database have a difference of 65536 or 2^16,
Using the prepared statement api for getting data from the database works properly as far as I can tell.
Any help as to what is going on here would be appreciated.
Thanks,