Mysql,检查表和C的行数

时间:2011-12-17 23:13:19

标签: mysql c

我尝试使用函数来检查表是否存在并获得总行数,但我得到结果1而不是99999。 那么,我该怎么办呢?

int sql_table_length(char* database, char* dtable, char* mysql_user_name, char* mysql_password)
{
    int retval = 0;
    MYSQL *conn;
    conn = mysql_init(NULL);
    if (conn)
    {
        if (mysql_real_connect(conn, "localhost", mysql_user_name, mysql_password, database, 0, NULL, 0)!=0)
        {
            char chktable[512] = {0};
            sprintf(chktable,"%s%s%s", "SHOW TABLES LIKE '", dtable, "'");
            mysql_query(conn, chktable);
            if (mysql_store_result(conn))
            {
                MYSQL_RES *result;
                char lngtable[512] = {0};
                sprintf(lngtable, "%s%s", "SELECT COUNT(*) FROM ", dtable);
                if (!(mysql_query(conn, lngtable)))
                {
                    result = mysql_store_result(conn);
                    retval = mysql_num_rows(result); // here I get 1
                    mysql_free_result(result);
                }
                else retval = -4;   //no rows
            }
            else retval = -3;       //don't exist
        }
        else retval = -2;           //can't connect
    }
    else retval = -1;               //no connection
    mysql_close(conn);
    return retval;
}

谢谢!

2 个答案:

答案 0 :(得分:2)

您的查询SELECT COUNT(*) FROM table返回包含表中行数的单行。您需要查询返回的数字,而不是计算结果中的行数。

你会想做(未经测试)的事情:

MYSQL_STMT *stmt;
MYSQL_BIND bind;
long rows;
my_bool error; /* Omit at your own risk */

stmt = mysql_stmt_init(mysql)
mysql_stmt_prepare(stmt, lngtable, strlen(lngtable));
mysql_stmt_execute(stmt);

memset(&bind, 0, sizeof(bind));
bind.buffer_type = MYSQL_TYPE_LONG;
bind.buffer = (char *)&rows;
bind.error = &error; /* Omit at your own risk */

mysql_stmt_bind_result(stmt, &bind);

mysql_stmt_fetch(stmt);
printf("Number of rows: %ld\n", rows);

您应该为所有mysql次来电添加错误检查。有关绑定参数的更多示例,请参阅http://dev.mysql.com/doc/refman/5.0/en/mysql-stmt-fetch.html

或者,您可以将查询更改为SELECT * FROM table并保留其余代码,但这要求MySQL做更多工作,结果可能需要更长时间。

答案 1 :(得分:1)

在大卫的建议之后,我找到了更简单,更安全的方法来获得结果。也许有人会需要这个。

int sql_table_length(char* database, char* dtable, char* mysql_user_name, char* mysql_password)
{
    MYSQL *conn;
    MYSQL_RES *result;
    MYSQL_ROW rowdata;
    int retval = 0;
    conn = mysql_init(NULL);
    if (conn)
    {
        if (mysql_real_connect(conn, "localhost", mysql_user_name, mysql_password, database, 0, NULL, 0)!=0)
        {
            char chktable[512] = {0};
            sprintf(chktable,"%s%s%s", "SHOW TABLES LIKE '", dtable, "'");
            if (!(mysql_query(conn, chktable)))
            {
                result = mysql_store_result(conn);
                if (mysql_num_rows(result))
                {
                    char lngtable[512] = {0};
                    sprintf(lngtable, "%s%s", "SELECT COUNT(*) FROM ", dtable);
                    if (!(mysql_query(conn, lngtable)))
                    {
                        result = mysql_store_result(conn);
                        if (result)
                        {
                            rowdata = mysql_fetch_row(result);
                            if (rowdata)
                                retval = atoi(rowdata[0]);
                            else retval = -7;   // data error
                        }
                        else retval = -6;       // result error
                        mysql_free_result(result);
                    }
                    else retval = -5;           // can't select
                }
                else retval = -4;               // table don't exist
            }
            else retval = -3;                   // can't query
        }
        else retval = -2;                       // can't connect
    }
    else retval = -1;                           // no server connection
    mysql_close(conn);
    return retval;
}

欢迎任何关于可能的内存泄漏(或其他)的评论!