Like Tree4Likes
  • 1 Post By Salem
  • 2 Post By rogster001
  • 1 Post By rags_to_riches

Help me to fix problem

This is a discussion on Help me to fix problem within the C Programming forums, part of the General Programming Boards category; My project: Write a C program to connect to mysql database then query and show the result on screen Code: ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    6

    Help me to fix problem

    My project: Write a C program to connect to mysql database then query and show the result on screen
    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);
        }
    }
    1>------ Build started: Project: abc, Configuration: Debug Win32 ------1>Build started 11/16/2012 2:04: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>c:\users\onggia.hehe\documents\visual studio 2010\projects\test1\abc\1.c(169): error C2143: syntax error : missing ';' before 'type'
    1>c:\users\onggia.hehe\documents\visual studio 2010\projects\test1\abc\1.c(173): error C2143: syntax error : missing ';' before 'type'
    1>
    1>Build FAILED.
    1>
    1>Time Elapsed 00:00:00.34
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Help me to fix.Thank all
    Last edited by athena21191; 11-16-2012 at 12:30 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Seems to me an inability to spell as much as anything.
    1>c:\users\onggia.hehe\documents\visual studio 2010\projects\test1\abc\1.c(29): error C2065: 'NULNULL' : undeclared identifier
    Try NULL

    1>c:\users\onggia.hehe\documents\visual studio 2010\projects\test1\abc\1.c(63): error C2065: 'sstdin' : undeclared identifier
    Try stdin

    > 1>c:\users\onggia.hehe\documents\visual studio 2010\projects\test1\abc\1.c(141): error C2065: 'stdstdout' : undeclared identifier
    Try stdout
    athena21191 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    Quote Originally Posted by Salem View Post
    Seems to me an inability to spell as much as anything.
    1>c:\users\onggia.hehe\documents\visual studio 2010\projects\test1\abc\1.c(29): error C2065: 'NULNULL' : undeclared identifier
    Try NULL

    1>c:\users\onggia.hehe\documents\visual studio 2010\projects\test1\abc\1.c(63): error C2065: 'sstdin' : undeclared identifier
    Try stdin

    > 1>c:\users\onggia.hehe\documents\visual studio 2010\projects\test1\abc\1.c(141): error C2065: 'stdstdout' : undeclared identifier
    Try stdout
    Thanks so much.What about 5 errors above?
    I'm just a newbie so the project is too difficult

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Dunno - learn some C and learn to fix your own code.

    If you want to learn C, then present your own code and we're happy to help.

    But drive-by posters looking to score a quick fix from some random piece of crappy code found on the net are really not worth the effort.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    Quote Originally Posted by Salem View Post
    Dunno - learn some C and learn to fix your own code.

    If you want to learn C, then present your own code and we're happy to help.

    But drive-by posters looking to score a quick fix from some random piece of crappy code found on the net are really not worth the effort.
    Thanks.I will try to learn later.
    But tomorrow is the deadline to complete my project
    Please help me one more

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    Thanks.I will try to learn later
    So you are basically saying - yea whatever - am not interested in writing anything myself, i just want you to fix up this piece of garbage code i found on the interweb for me so i can pass it off as my own work in college and get away with not doing any study at all myself

    i find that quite annoying - if not insulting
    Salem and rags_to_riches like this.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    so i can pass it off as my own work in college and get away with not doing any study at all myself
    Or maybe even worse: to get a job!
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  4. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21