#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    [Help] Problem with C program


    My project: write a C program to connect to mysql
    Code:
    #include <WinSock.h>
    #include <mysql.h> 
    #include <stdlib.h> 
    #include <stdio.h> 
    #include <password.hh> 
    
    MYSQL     *conn;
    MYSQL_RES *res;
    MYSQL_ROW  row;
    
    unsigned long how_rows (void);
    void print_data(void);
    char* to_upper (char*);
    void print_table(void);
    void print_line (void);
    
    //----------------------------------------------------------
     
    int main (int argc, char* argv[])
    {
        unsigned long ul;
        int i;
        char databases[25][30];
        char query[30];
        char temp[30], table[30];
        
        conn = mysql_init ( NULL );
        mysql_real_connect (conn,"","root",password(),
                "",3306,NULL);
    
        mysql_query(conn,"show databases");
        res = mysql_store_result(conn);
    
        ul = how_rows();
        i=0;
        system("clear");
        printf("\nThere are %lu databases\n",ul);
        puts("=====================");
    
        while ( row = mysql_fetch_row(res) )
        {
            strcpy(databases[i],row[0]);
            puts(databases[i]);
            i++;
        }
        for ( i = 0; i < ul; i++ )
        {
            if ( strcmp(databases[i],"mysql") == 0 )
                continue;
            sprintf(query,"use %s",databases[i]);
            mysql_query(conn,query);
            strcpy(temp,databases[i]);
            fprintf(stdout,"\nThe database %s consists tables:\n",
                    to_upper(temp));
            puts("==========================================");
            print_data();
            getchar();
    //        usleep(900000); 
        }
        while(1)
        {
            fprintf(stdout,"What database do you want to use? (0-quit): ");
            fgets(temp,29,stdin);
            temp[strlen(temp)-1] = '\0';
    
            if ( strcmp(temp,"0") == 0 )
                break;
            for ( i = 0; i < ul; i++ )
            {
                if ( strcmp(temp,databases[i]) == 0 )
                {
                    while(1)
                    {
                        fprintf(stdout,"What table do you want to see? ");
                        fprintf(stdout,"(0-quit): ");
                        fgets(table,29,stdin);
                        table[strlen(table)-1] = '\0';
    
                        if ( strcmp(table,"0") == 0 )
                            break;
                        sprintf(query,"select * from %s.%s",temp,table);
                        mysql_query(conn,query);
                        print_table();
                    }
                }
            }
        }    
        mysql_close(conn);
        exit(0);
    }
    //----------------------------------------------------------  
    unsigned long how_rows (void)
    //----------------------------------------------------------  
    {
        return (unsigned long) mysql_num_rows(res);
    }
    //----------------------------------------------------------  
    void print_data(void)
    //----------------------------------------------------------  
    {
        mysql_query(conn,"show tables");
        res = mysql_store_result(conn);
        while ( row = mysql_fetch_row(res) )
        {
            fprintf(stdout,"%s\n",row[0]);
        }
    }
    //----------------------------------------------------------  
    char* to_upper (char* name)
    //----------------------------------------------------------  
    {
        char *p = name;
        while (*p)
        {
            *p = toupper(*p);
            p++;
        }
        return name;
    }
    //----------------------------------------------------------  
    void print_table(void)
    //----------------------------------------------------------  
    {
        MYSQL_FIELD *field;
        unsigned int i, j, column_length;
        
        res = mysql_store_result(conn);
        
        mysql_field_seek(res,0);
        for ( i = 0; i < mysql_num_fields(res); i++ )
        {
            field = mysql_fetch_field(res);
            column_length = strlen(field->name);
            if ( column_length < field->max_length )
                column_length = field->max_length;
            if ( column_length < 4 && !IS_NOT_NULL(field->flags) )
                column_length = 4;
            field->max_length = column_length;
        }
        print_line();
        fputc('\n',stdout);
        mysql_field_seek(res,0);
        for ( i = 0; i < mysql_num_fields(res); i++ )
        {
            field = mysql_fetch_field(res);
            fprintf(stdout," %-*s ",field->max_length,field->name);
        }
        fputc('\n',stdout );   print_line();
        fputc('\n',stdout);
        while ((row = mysql_fetch_row(res)) != NULL)
        {
            mysql_field_seek(res,0);
    
            for ( i = 0; i < mysql_num_fields(res); i++ )
            {
                field = mysql_fetch_field(res);
                if ( row[i] == NULL )
                    fprintf(stdout," %-*s ",field->max_length,"NULL");
                else
                if ( IS_NUM(field->type) )
                    fprintf(stdout," %*s ",field->max_length,row[i]);
                else
                    fprintf(stdout," %-*s ",field->max_length,row[i]);
            }
            fputc('\n',stdout);    }
        fputc('\n',stdout); {   mysql_free_result(res);
    }
    //---------------------------------------------------------- 
    void print_line (void)
    //----------------------------------------------------------  
    {
        MYSQL_FIELD *field;
        unsigned int i, j;
        
        mysql_field_seek(res,0);
        for ( i = 0; i < mysql_num_fields(res); i++ )
        {
            field = mysql_fetch_field(res);
            for ( j = 0; j < field->max_length + 2; j++ )
                fputc('=',stdout);
        }
    }
    and some errors
    1>------ Build started: Project: abc, Configuration: Debug Win32 ------
    1>Build started 11/16/2012 2:23:57 PM.
    1>InitializeBuildStatus:
    1> Touching "Debug\abc.unsuccessfulbuild".
    1>ClCompile:
    1> 1.c
    1>c:\users\onggia.hehe\documents\visual studio 2010\projects\test1\abc\1.c(29): error C2198: 'mysql_real_connect' : too few arguments for call
    1>c:\users\onggia.hehe\documents\visual studio 2010\projects\test1\abc\1.c(46): warning C4018: '<' : signed/unsigned mismatch
    1>c:\users\onggia.hehe\documents\visual studio 2010\projects\test1\abc\1.c(68): warning C4018: '<' : signed/unsigned mismatch

    1>
    1>Build FAILED.
    1>
    1>Time Elapsed 00:00:00.66
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Comments on this post

    • salem disagrees : You forgot to give credit for your sources - http://cboard.cprogramming.com/cplusplus-programming/152339-help-me-fix-problem.html
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0
    Seems like you are missing an argument in your command to mysql_real_connect(). I also think you should be providing values to the host to connect to and such, I think its clearer to use "localhost" than "".

    This is the actual code for the call. I think you should pass a null to the unix_socket value.

    Code:
    MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag)
    Also, you are missing a bracket at the end of the print_table() function.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by Murzy
    Seems like you are missing an argument in your command to mysql_real_connect(). I also think you should be providing values to the host to connect to and such, I think its clearer to use "localhost" than "".

    This is the actual code for the call. I think you should pass a null to the unix_socket value.

    Code:
    MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag)
    Also, you are missing a bracket at the end of the print_table() function.
    Thanks so much.But i dont't know how to fix those :chomp:
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,214
    Rep Power
    2222
    Originally Posted by athena21191
    Thanks so much.But i dont't know how to fix those
    Huh? How could that be since he just told you exactly what you needed to do?

    Although your formatting is in general good, in print_table you reverted to a worst-than-crap formatting and as a result you fell victim to your own bad practices. Here's how it looks when properly formatted (I'm also adding comments on the close braces starting with the while loop):
    Code:
    //----------------------------------------------------------  
    void print_table(void)
    //----------------------------------------------------------  
    {
        MYSQL_FIELD *field;
        unsigned int i, j, column_length;
        
        res = mysql_store_result(conn);
        
        mysql_field_seek(res,0);
        for ( i = 0; i < mysql_num_fields(res); i++ )
        {
            field = mysql_fetch_field(res);
            column_length = strlen(field->name);
            if ( column_length < field->max_length )
                column_length = field->max_length;
            if ( column_length < 4 && !IS_NOT_NULL(field->flags) )
                column_length = 4;
            field->max_length = column_length;
        }  
        print_line();
        fputc('\n',stdout);
        mysql_field_seek(res,0);
        for ( i = 0; i < mysql_num_fields(res); i++ )
        {
            field = mysql_fetch_field(res);
            fprintf(stdout," %-*s ",field->max_length,field->name);
        }
        fputc('\n',stdout );   print_line();
        fputc('\n',stdout);
        while ((row = mysql_fetch_row(res)) != NULL)
        {
            mysql_field_seek(res,0);
    
            for ( i = 0; i < mysql_num_fields(res); i++ )
            {
                field = mysql_fetch_field(res);
                if ( row[i] == NULL )
                    fprintf(stdout," %-*s ",field->max_length,"NULL");
                else
                if ( IS_NUM(field->type) )
                    fprintf(stdout," %*s ",field->max_length,row[i]);
                else
                    fprintf(stdout," %-*s ",field->max_length,row[i]);
            }  // end for
            fputc('\n',stdout);    
        }  // end while
        fputc('\n',stdout); 
        {  // start mysteriously extraneous code block
            mysql_free_result(res);
        // the end mysteriously extraneous code block brace is missing
    }  // end print_table, misinterpreted as end extraneous code block
    //  to the compiler, there is no close brace for the function
    You've chosen a good indenting style that keeps braces from being hidden and which brings everything out where you can easily check that all braces are in matching pairs. But the best style in the world is of no use if you neglect to actually use it.
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,403
    Rep Power
    1871
    > Huh? How could that be since he just told you exactly what you needed to do?
    The problem is the OP is clueless.

    The code was blindly copied from here. But that web page author made a complete pigs-ear of turning code into HTML, so the result was a somewhat broken mess.

    Which wouldn't be much of a problem for anyone where clues > 0.

    The upside, it filters out the dim copy/paste brigade from simply handing in finished code. The downside is they come here looking for someone to edit the code to the point where it can be copy/pasted to the assignment answer sheet.

    A lot of the low hanging fruit was picked here until we all realised that we were doing all the work.

    So now, it's a case of fix remaining problems by posting on successive forums, hoping to glean a fix or two before moving on.

    Comments on this post

    • ptr2void agrees : So tired of this. These people are pathetic.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo