By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,312 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,312 IT Pros & Developers. It's quick & easy.

Mysql C prepared statement API sending incorrect data

P: n/a
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);

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 216,

Using the prepared statement api for getting data from the database works
properly as far as I can tell. So does the standard C interface, as well as
the perl and php interfaces

Any help as to what is going on here would be appreciated.

Thanks,
Oct 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.