MySQL 使用 C API 准备语句中的语法错误
Syntax error in MySQL prepare statement using C API
我创建了一个 MySQL 数据库,其中包含一些存储过程。使用 MySQL Workbench SP fork 很好,现在我需要使用 c 程序启动它们。
我创建了成功连接到我的数据库的程序,并且我能够启动不需要参数的程序。
要启动更复杂的过程,我需要在 c: 中使用 prepare 语句,特别是,我想调用过程 esame_cancella(IN code CHAR(5))
删除 table [=29= 的选定行].
int status;
MYSQL_RES *result;
MYSQL_ROW row;
MYSQL_FIELD *field;
MYSQL_RES *rs_metadata;
MYSQL_STMT *stmt;
MYSQL_BIND ps_params[6];
unsigned long length[6];
char cod[64];
printf("Codice: ");
scanf ("%s",cod);
length[0] = strlen(cod);
stmt = mysql_stmt_init(conn);
if (stmt == NULL) {
printf("Could not initialize statement\n");
exit(1);
}
status = mysql_stmt_prepare(stmt, "call esame_cancella(?) ", 64);
test_stmt_error(stmt, status); //line which gives me the syntax error
memset(ps_params, 0, sizeof(ps_params));
ps_params[0].buffer_type = MYSQL_TYPE_VAR_STRING;
ps_params[0].buffer = cod;
ps_params[0].buffer_length = 64;
ps_params[0].length = &length[0];
ps_params[0].is_null = 0;
// bind parameters
status = mysql_stmt_bind_param(stmt, ps_params); //muore qui
test_stmt_error(stmt, status);
// Run the stored procedure
status = mysql_stmt_execute(stmt);
test_stmt_error(stmt, status);
}
我用test_stmt_error
看MySQL日志调用程序。
static void test_stmt_error(MYSQL_STMT * stmt, int status)
{
if (status) {
fprintf(stderr, "Error: %s (errno: %d)\n",
mysql_stmt_error(stmt), mysql_stmt_errno(stmt));
exit(1);
}
}
当我编译并启动我的程序时,我有以下日志:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 (errno: 1064)
有什么帮助吗?
似乎传递给 mysql_stmt_prepare
的字符串长度是错误的 - 尝试将 64 更改为 24。
或者更好的方法是:
const char sql_sp[] = "call esame_cancella(?) ";
...
status = mysql_stmt_prepare(stmt, sql_sp, sizeof(sql_sp));
我创建了一个 MySQL 数据库,其中包含一些存储过程。使用 MySQL Workbench SP fork 很好,现在我需要使用 c 程序启动它们。
我创建了成功连接到我的数据库的程序,并且我能够启动不需要参数的程序。
要启动更复杂的过程,我需要在 c: 中使用 prepare 语句,特别是,我想调用过程 esame_cancella(IN code CHAR(5))
删除 table [=29= 的选定行].
int status;
MYSQL_RES *result;
MYSQL_ROW row;
MYSQL_FIELD *field;
MYSQL_RES *rs_metadata;
MYSQL_STMT *stmt;
MYSQL_BIND ps_params[6];
unsigned long length[6];
char cod[64];
printf("Codice: ");
scanf ("%s",cod);
length[0] = strlen(cod);
stmt = mysql_stmt_init(conn);
if (stmt == NULL) {
printf("Could not initialize statement\n");
exit(1);
}
status = mysql_stmt_prepare(stmt, "call esame_cancella(?) ", 64);
test_stmt_error(stmt, status); //line which gives me the syntax error
memset(ps_params, 0, sizeof(ps_params));
ps_params[0].buffer_type = MYSQL_TYPE_VAR_STRING;
ps_params[0].buffer = cod;
ps_params[0].buffer_length = 64;
ps_params[0].length = &length[0];
ps_params[0].is_null = 0;
// bind parameters
status = mysql_stmt_bind_param(stmt, ps_params); //muore qui
test_stmt_error(stmt, status);
// Run the stored procedure
status = mysql_stmt_execute(stmt);
test_stmt_error(stmt, status);
}
我用test_stmt_error
看MySQL日志调用程序。
static void test_stmt_error(MYSQL_STMT * stmt, int status)
{
if (status) {
fprintf(stderr, "Error: %s (errno: %d)\n",
mysql_stmt_error(stmt), mysql_stmt_errno(stmt));
exit(1);
}
}
当我编译并启动我的程序时,我有以下日志:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 (errno: 1064)
有什么帮助吗?
似乎传递给 mysql_stmt_prepare
的字符串长度是错误的 - 尝试将 64 更改为 24。
或者更好的方法是:
const char sql_sp[] = "call esame_cancella(?) ";
...
status = mysql_stmt_prepare(stmt, sql_sp, sizeof(sql_sp));