Re: C API Prepared Statement Parameter not bind
Posted by: Armin Inauen
Date: November 21, 2005 04:53AM

I made some more test:
First I created a table with

CREATE TABLE `sbtest` (
`ID` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci

SHOW COLUMNS FROM sbtest;

ID int(11)


Add some data to it to get the following:

SELECT * FROM sbtest;

ID
============
1
2
3
4


now I tried to fetch data with mysql_stmt_fetch:

====================================================================
#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;
long int int_data = 3;
long 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", "root", "", "test", 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;

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;
}
===============================================

The program comiles and runs without any error or warnings, but doesn't get back any
data with mysql_stmt_fetch.

as I mention before, if I set
#define QUERY "SELECT ID FROM sbtest WHERE ID=3"

I get the right result.

it seems that my params do not get bind, but I don't know why.
armin

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: C API Prepared Statement Parameter not bind
649
November 21, 2005 04:53AM


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.