LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   printing database column name so much times. (https://www.linuxquestions.org/questions/linux-software-2/printing-database-column-name-so-much-times-4175686406/)

rahulvishwakarma 12-07-2020 02:52 AM

printing database column name so much times.
 
1 Attachment(s)
hi to all, i am building a small project using mysql as back end in server ( cenos 6.10 ) and client as c++ in client machine( centos 7.5). Now i stuck in following code as it is showing lines after end of
rows of mysql tables based on mysql Connection and result set. output is showing in image :-
Code:


int sqlprocess::process_result_set(MYSQL *mysql, MYSQL_RES *res )
{
    MYSQL_ROW row;
    MYSQL_FIELD *field;
    unsigned columnLen;
    unsigned int i;

    int offset1 = 1;
    int offset2 = 0;
    int totallen = 0;
    int times = 0;
    int j = 0;
    int l = 0;
    int intlines = 0;
    int intl = 0;

    mysql_field_seek(res, 0);

    struct winsize w;

    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);


    int numf = mysql_num_fields(res);

        for(int i = 0; i < numf; i++ )
    {
        field = mysql_fetch_field(res);
        columnLen = strlen(field->name);
        if(columnLen < field->max_length)
        {
          columnLen = field->max_length;
        }
        if(columnLen < 4 && !IS_NOT_NULL(field->flags))
        {
            columnLen = 4;
        }
        field->max_length = columnLen;
        totallen += columnLen;
    }

    offset1 = totallen;
    k = 8;

    do
    {
        printDashes(res, 0);
        mysql_field_seek(res, 0);

        gotoxy(l, k);
        cout << "|";
        int nums = mysql_num_fields(res);
        for(int i = 0; i < nums; i++)
        {
            field = mysql_fetch_field(res);
            cout << std::setw(field->max_length) << field->name << "|";
        }

        k++;
        gotoxy(l, k);
        printDashes(res, l);

        if(times > 0)
        {

            l = totallen * times + 7;
        }
        else
        {
            l = totallen*times;
        }

        while ((row = mysql_fetch_row(res)) != nullptr && k < w.ws_row)
        {
            intlines++;
            if(times > 0)
            {

                l = totallen * times + 7;
            }
            else
            {
                l = totallen*times;
            }
            gotoxy(l, ++k);
            mysql_field_seek(res, 0);
            cout << "|";
            for(int i = 0; i < mysql_num_fields(res); i++)
            {
                field = mysql_fetch_field(res);
                if(row[i] == nullptr)
                {
                    cout << std::setw(field->max_length) << "NULL" << "|";
                }
                else
                {
                    cout << std::setw(field->max_length) << row[i] << "|";
                }
            }
            if( k >= w.ws_row)
            {
                times++;
                k = 8;
                gotoxy(l, k);
            }
        }

        gotoxy(l + 7, intl +1);
        l = mysql_num_rows(res);
    }while(k < w.ws_row && intlines <= l);
    cout << "No of affected rows : " << l;
    cin.get();
    printDashes(res, l+7);

    return l;
}


TB0ne 12-07-2020 07:52 AM

Quote:

Originally Posted by rahulvishwakarma (Post 6192588)
hi to all, i am building a small project using mysql as back end in server ( cenos 6.10 ) and client as c++ in client machine( centos 7.5). Now i stuck in following code as it is showing lines after end of rows of mysql tables based on mysql Connection and result set. output is showing in image :-
Code:

int sqlprocess::process_result_set(MYSQL *mysql, MYSQL_RES *res )
{
    MYSQL_ROW row;
    MYSQL_FIELD *field;
    unsigned columnLen;
    unsigned int i;

    int offset1 = 1;
    int offset2 = 0;
    int totallen = 0;
    int times = 0;
    int j = 0;
    int l = 0;
    int intlines = 0;
    int intl = 0;

    mysql_field_seek(res, 0);

    struct winsize w;

    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);


    int numf = mysql_num_fields(res);

        for(int i = 0; i < numf; i++ )
    {
        field = mysql_fetch_field(res);
        columnLen = strlen(field->name);
        if(columnLen < field->max_length)
        {
          columnLen = field->max_length;
        }
        if(columnLen < 4 && !IS_NOT_NULL(field->flags))
        {
            columnLen = 4;
        }
        field->max_length = columnLen;
        totallen += columnLen;
    }

    offset1 = totallen;
    k = 8;

    do
    {
        printDashes(res, 0);
        mysql_field_seek(res, 0);

        gotoxy(l, k);
        cout << "|";
        int nums = mysql_num_fields(res);
        for(int i = 0; i < nums; i++)
        {
            field = mysql_fetch_field(res);
            cout << std::setw(field->max_length) << field->name << "|";
        }

        k++;
        gotoxy(l, k);
        printDashes(res, l);

        if(times > 0)
        {

            l = totallen * times + 7;
        }
        else
        {
            l = totallen*times;
        }

        while ((row = mysql_fetch_row(res)) != nullptr && k < w.ws_row)
        {
            intlines++;
            if(times > 0)
            {

                l = totallen * times + 7;
            }
            else
            {
                l = totallen*times;
            }
            gotoxy(l, ++k);
            mysql_field_seek(res, 0);
            cout << "|";
            for(int i = 0; i < mysql_num_fields(res); i++)
            {
                field = mysql_fetch_field(res);
                if(row[i] == nullptr)
                {
                    cout << std::setw(field->max_length) << "NULL" << "|";
                }
                else
                {
                    cout << std::setw(field->max_length) << row[i] << "|";
                }
            }
            if( k >= w.ws_row)
            {
                times++;
                k = 8;
                gotoxy(l, k);
            }
        }

        gotoxy(l + 7, intl +1);
        l = mysql_num_rows(res);
    }while(k < w.ws_row && intlines <= l);
    cout << "No of affected rows : " << l;
    cin.get();
    printDashes(res, l+7);

    return l;
}


Ok...since you wrote your code, can you go back and comment it, so it can be easily read, before asking others to debug your program for you?? And you don't tell us what's in the database, how many records, etc., so we can't tell you if you're outputting SOMETHING (or nothing), nor how you want your program to work.

Suggest you read this: https://www.informit.com/articles/ar...30494&seqNum=6

...and apply the information to your program. If you'd like help with your code, comment it, and ask a clear question. May also want to check several of your other threads where you've been working on this for some time:
https://www.linuxquestions.org/quest...board-41756826
https://www.linuxquestions.org/quest...-a-4175682559/
https://www.linuxquestions.org/quest...27-4175673488/
https://www.linuxquestions.org/quest...ks-4175673022/


All times are GMT -5. The time now is 05:35 AM.