LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-07-2020, 02:52 AM   #1
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 138

Rep: Reputation: 2
printing database column name so much times.


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;
}
Attached Thumbnails
Click image for larger version

Name:	cbs row overflow.png
Views:	19
Size:	254.1 KB
ID:	34789  
 
Old 12-07-2020, 07:52 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,553

Rep: Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946
Quote:
Originally Posted by rahulvishwakarma View Post
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/

Last edited by TB0ne; 12-07-2020 at 07:55 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert two column output into 4(+) column output beeblequix Linux - Enterprise 4 09-26-2011 08:41 PM
awk multiple column into single column ilukacevic Programming 49 07-19-2010 07:23 PM
Read text file column by column RVF16 Programming 11 05-31-2009 07:16 AM
Concatenate column 1 and column 2 of related lines cgcamal Programming 4 11-20-2008 10:43 AM
Database Programming (database to database transaction) johncsl82 Programming 7 02-02-2007 08:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration