1

I try to make function to check if table exists and to get total number of rows but I get result 1 instead of 99999. So, what should I do here to work properly?

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;
}

Thanks!

2 Answers 2

2

Your query, SELECT COUNT(*) FROM table returns a single row containing the number of rows in the table. Instead of counting the number of rows in the result, you want to query the number that is returned.

You will want to do something like (untested):

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);

You should add error checking to all the mysql calls. See http://dev.mysql.com/doc/refman/5.0/en/mysql-stmt-fetch.html for more examples about binding parameters.

Alternatively, you can change your query to something like SELECT * FROM table and keep the rest of your code, but this is asking MySQL to do a lot more work, and the result will likely take longer.

8
  • But how can I get this number in retval? And can I get it with my code? Of course, I would like faster solution.
    – Wine Too
    Dec 17, 2011 at 23:47
  • I added some sample code; you want to put this stuff after your sprintf(lngtable, ...) call in place of your mysql_query call. Dec 17, 2011 at 23:59
  • WOW, so complicated? I get few errors: /home/name/Desktop/_c/_mySqlGUI/mysqlgui/my_main.c||In function ‘sql_table_lng’:| /home/name/Desktop/_c/_mySqlGUI/mysqlgui/my_main.c|716|error: incompatible type for argument 2 of ‘mysql_stmt_bind_result’| /usr/include/mysql.h|690|note: expected ‘struct MYSQL_BIND *’ but argument is of type ‘MYSQL_BIND’| /home/name/Desktop/_c/_mySqlGUI/mysqlgui/my_main.c|687|warning: unused variable ‘result’ [-Wunused-variable]| ||=== Build finished: 2 errors, 1 warnings ===|
    – Wine Too
    Dec 18, 2011 at 0:11
  • You can remove the initialization of bind.is_null, bind.length, and possibly bind.error (if you don't care about getting errors), as well as the declarations that they have. The MySQL C API is rather verbose, and you will need lots of code when you work with it. Higher-level languages like Perl and PHP abstract away a lot of this stuff for you so that you don't have to worry about it, but the C API is pretty raw. Dec 18, 2011 at 0:16
  • 1
    @user973238: I fixed the code, I had forgotten an & before bind in the mysql_stmt_bind_result call. I also removed some of the extra code I had talked about above. Dec 18, 2011 at 0:19
1

After David's suggestion, I found a simpler and safer way to get the result. Maybe someone will need this:

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;
}

Any remarks on possible memory leaks (or other) are welcome!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.