C API Prepared Statement Parameter not bind
Posted by: Armin Inauen
Date: November 08, 2005 03:04AM

Hello
I'm using MySql 5.0.15
I tried to run the oltp test from sysbench. It generated the Database, but never returned result.
I wrote a small test programm:

=========================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
#include <errmsg.h>

#define QUERY "SELECT id FROM sbtest WHERE id=?"

int main(int argc, char *argv[])
{
MYSQL *mysql;
MYSQL_STMT *stmt;
MYSQL_BIND bind[1];
MYSQL_BIND result[1];
MYSQL_RES *prepare_meta_result;
int int_data = 10;
int int_result = 0;
int affecteds_rows = 0;
int column_count;
int param_count;
unsigned long length;
int is_null;

mysql = mysql_init(NULL);
mysql = mysql_real_connect(mysql, "192.168.0.1", "bench", "mysql", "sbtest", 3306, NULL, 0);

stmt = mysql_stmt_init(mysql);

if (mysql_stmt_prepare(stmt, QUERY, strlen(QUERY)))
{
printf("error\n");
}
param_count = mysql_stmt_param_count(stmt);
bzero((char*) bind, sizeof(bind));
bind[0].buffer_type = MYSQL_TYPE_LONG;
bind[0].buffer = (char *)&int_data;
bind[0].is_null = 0;
bind[0].length = 0;
int_data = 10;

if (mysql_stmt_bind_param(stmt, bind))
{
printf("error\n");
}

prepare_meta_result = mysql_stmt_result_metadata(stmt);
column_count= mysql_num_fields(prepare_meta_result);
if (mysql_stmt_execute(stmt))
{
printf("error\n");
}
bzero((char*) result, sizeof(result));
result[0].buffer_type = MYSQL_TYPE_LONG;
result[0].buffer = (char *)&int_result;
result[0].is_null = &is_null;
result[0].length = &length;
if (mysql_stmt_bind_result(stmt, result))
{
printf("error\n");
}
if (mysql_stmt_store_result(stmt))
{
printf("error\n");
}
while (!mysql_stmt_fetch(stmt))
{
printf("id = %i\n", int_result);
}

mysql_free_result(prepare_meta_result);
mysql_stmt_close(stmt);
mysql_close(mysql);

return EXIT_SUCCESS;
}
===============================================================

param_count returns 1
mysql_stmt_fetch doesn't get any result back.

If I define:
#define QUERY "SELECT id FROM sbtest WHERE id=10"

param_count returns 0
mysql_stmt_fetch gets bach one result.

Why does my parameter bind not work?

regards
Armin

Options: ReplyQuote


Subject
Views
Written By
Posted
C API Prepared Statement Parameter not bind
956
November 08, 2005 03:04AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.