Description:
Hello, I'am trying run a simple example from prepared statements using the C API and I have arrived to conclusion that is impossible.
Is very simple, I create, prepare, execute and fetch one prepare statement and later i want do the same with another prepared statement maintaining the first opened for fetch it, I don't store results because like say the reference manual I can fetch row by row.
How to repeat:
Teh code is :
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <my_global.h>
#include <mysql.h>
#define def_host_name "localhost" /* host to connect to (default = localhost) */
#define def_user_name "root" /* user name (default = your login name) */
#define def_password "guacha" /* password (default = none) */
#define def_db_name "sonybcd" /* database to use (default = none) */
int main(int argc, char *argv[])
{
MYSQL *conn; /* pointer to connection handler */
MYSQL_STMT *stmnt, *stmnt2;
MYSQL_BIND vars_bind;
int i=0, z=0;
char area[11];
char query1[150], query2[150];
strcpy(query1,"SELECT AREA FROM pepe");
strcpy(query2,"SELECT AREA FROM jose");
memset(&vars_bind,0,sizeof(vars_bind));
memset(&stmnt,0,sizeof(stmnt));
memset(&stmnt2,0,sizeof(stmnt2));
memset(&conn,0,sizeof(conn));
if (!(conn = mysql_init (NULL)))
{
fprintf (stderr, "mysql_init() failed (probably out of memory)\n");
exit (1);
}
printf("Creada conexión.\n");
if (!mysql_real_connect (conn,def_host_name,def_user_name,def_password,def_db_name,0,NULL,0))
{
fprintf(stderr, "mysql_real_connect() failed:\nError %u (%s)\n",mysql_errno(conn), mysql_error(conn));
exit (1);
}
printf("Conectado a %s.\n",mysql_get_host_info(conn));
printf("MySQL client version: %s\n", mysql_get_client_info());
if (!(stmnt2 = mysql_stmt_init(conn)))
{
fprintf (stderr, "mysql_stmt_init() failed (probably out of memory)\n");
exit (1);
}
if (mysql_stmt_prepare(stmnt2, query1, strlen(query1)))
{
fprintf(stderr, "mysql_stmt_prepare() failed:\nError %u (%s)\n",mysql_stmt_errno(stmnt2), mysql_stmt_error(stmnt2));
exit (1);
}
if (!(stmnt = mysql_stmt_init(conn)))
{
fprintf (stderr, "mysql_stmt_init() failed (probably out of memory)\n");
exit (1);
}
if (mysql_stmt_prepare(stmnt, query2, strlen(query2)))
{
fprintf(stderr, "mysql_stmt_prepare() failed:\nError %u (%s)\n",mysql_stmt_errno(stmnt), mysql_stmt_error(stmnt));
exit (1);
}
if (mysql_stmt_execute(stmnt2))
{
fprintf(stderr, "mysql_stmt_execute() failed:\nError %u (%s)\n",mysql_stmt_errno(stmnt2), mysql_stmt_error(stmnt2));
exit (1);
}
//In this point I obtain the error 2014 "Commands out of sync; you can't run this command now"
if (mysql_stmt_execute(stmnt))
{
fprintf(stderr, "mysql_stmt_execute() failed:\nError %u (%s)\n",mysql_stmt_errno(stmnt), mysql_stmt_error(stmnt));
exit (1);
}
if (mysql_stmt_fetch(stmnt2))
{
fprintf(stderr, "mysql_stmt_fetch:\nError %u (%s)\n",mysql_stmt_errno(stmnt), mysql_stmt_error(stmnt));
exit (1);
}
while (!mysql_stmt_fetch(stmnt) && i < 10)
{
strcpy(area,"");
i++;
while (!mysql_stmt_fetch(stmnt2) && z < 3)
{
z++;
}
}
if (mysql_stmt_close(stmnt))
{
fprintf(stderr, "mysql_stmt_close() failed:\nError %u (%s)\n",mysql_stmt_errno(stmnt), mysql_stmt_error(stmnt));
exit (1);
}
if (mysql_stmt_close(stmnt2))
{
fprintf(stderr, "mysql_stmt_close() failed:\nError %u (%s)\n",mysql_stmt_errno(stmnt), mysql_stmt_error(stmnt));
exit (1);
}
mysql_close (conn);
printf("Cerrado..\n");
return EXIT_SUCCESS;
}
the tables are very simple, the two are identical like follows:
CREATE TABLE `jose` (
`ALMA` tinyint(4) NOT NULL,
`AREA` varchar(10) NOT NULL,
`NUMERO` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
Suggested fix:
I'm doing anything not correctly?
Allow more than one statement at time, I think that is needed for any simple program.