准备好的SELECT语句执行但总是返回0行

时间:2012-01-05 15:42:28

标签: mysql c

我已多次阅读文档,我在谷歌搜索过,我发现了很多例子,下面的代码是我研究的结果。 但现在我有一个大问题,我找不到解决方案来解决它。 下面的代码执行查询但不显示结果,它总是返回0行,而它必须返回2行...我不明白错误在哪里。

#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"

如果我执行这个查询并且显然在更多的情况下评论代码......它可以工作......

1 个答案:

答案 0 :(得分:1)

mysql_stmt_fetch有四个可能的返回值:0(成功),1(错误),MYSQL_NO_DATAMYSQL_DATA_TRUNCATED。你的循环终止于除成功之外的任何事情 - 但你不会检查实际发生了什么。您可能会返回0行,或者您可能会通过mysql_stmt_errno()mysql_stmt_error()最佳检查某些错误条件...