1
문서를 여러 번 읽었으며 Google에서 검색했으며 많은 예제를 발견했으며 아래 코드는 내 연구 결과입니다. 하지만 지금은 큰 문제가 있고 해결책을 찾지 못했습니다. 아래 코드는 쿼리를 실행하지만 결과를 표시하지 않고 2 행을 반환해야하는 동안 항상 0 행을 반환합니다 ... 어디에서 실수인지 이해하지 못합니다.Prepared SELECT 문은 실행되지만 항상 0 행을 반환합니다.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mysql/mysql.h>
#define STRING_SIZE 50
#define SELECT_SAMPLE "select p.id_paz from pazienti p where p.id_doc = ?"
int main(void)
{
MYSQL *conn;
MYSQL_STMT *stmt;
MYSQL_BIND pbind[1],result[1]; /* results */
unsigned long length;
int row_count;
char login[STRING_SIZE];
my_bool is_null;
unsigned long plength;
char *pdata;
my_bool p_is_null;
// Open Database
const char *server = "localhost";
char *user = "root";
char *password = "password"; /* set me first */
char *database = "ProgettoSi";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
//...
}
// Allocate statement handler
stmt = mysql_stmt_init(conn);
if (mysql_stmt_prepare(stmt, SELECT_SAMPLE, strlen(SELECT_SAMPLE)))
{
//...
}
fprintf(stdout, " prepare, SELECT successful\n");
plength = STRING_SIZE * sizeof(char);
p_is_null = 0;
pdata = (char*)malloc(STRING_SIZE * sizeof(char));
/* STRING PARAMETER */
pbind[0].buffer_type= MYSQL_TYPE_STRING;
pbind[0].buffer= (char *)pdata;
pbind[0].buffer_length=STRING_SIZE * sizeof(char);
pbind[0].is_null= &p_is_null;
pbind[0].length= &plength;
if(mysql_stmt_bind_param(stmt, pbind)) {
//...
}
mysql_real_escape_string(conn, pdata, "123", strlen("123")*sizeof(char));
plength = strlen(pdata) + 1;
printf("Executing query with parameters %s. \n", pdata);
/* Execute the SELECT query */
if (mysql_stmt_execute(stmt))
{
//...
}
/* Bind the result buffers for all 4 columns before fetching them */
result[0].buffer_type= MYSQL_TYPE_STRING;
result[0].buffer= (char *)login;
result[0].buffer_length= STRING_SIZE;
result[0].is_null= &is_null;
result[0].length= &length;
if (mysql_stmt_bind_result(stmt, result))
{
//...
}
if (mysql_stmt_store_result(stmt))
{
//...
}
row_count= 0;
fprintf(stdout, "Fetching results ...\n");
while (!mysql_stmt_fetch(stmt))
{
row_count++;
fprintf(stdout, " row %d\n", row_count);
/* column 2 */
fprintf(stdout, " column1 (string) : ");
if (is_null)
fprintf(stdout, " NULL\n");
else
fprintf(stdout, " %s(%ld)\n", login, length);
}
/* Validate rows fetched */
fprintf(stdout, " total rows fetched: %d\n", row_count);
/* Close the statement */
if (mysql_stmt_close(stmt))
{
//...
}
return 0;
}
수동으로 값을 쿼리에 삽입하면 작동합니다.
#define SELECT_SAMPLE "select p.id_paz from pazienti p where p.id_doc = 123"
내가이 쿼리를 실행하고 분명 ... 작동 더의 코드를 언급하면 ...
을 통해 가장 좋은 오류 상태를 확인할 수 있습니다. 데이터베이스에 실제로 예상되는 데이터 행이 실제로 있는지 확인하십시오. 그렇다면 예제 코드를 절대 최소값으로 줄이면서 관찰 된 동작을 유지할 것을 제안합니다. 분명히 트리거되지 않은 많은 오류 검사가 있습니다. 그들은 예제에 기여하지 않았거나, 당신이 * 조건을 검사했음을 보여주기 위해 간단한'assert()'로 대체 될 수 있습니다 ... – DevSolar
예. 이중 체크를했습니다. 이제 예제 코드를 짧게 제안 해주세요. –