LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2021, 10:39 PM   #1
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 138

Rep: Reputation: 2
table iteration in C++ and mysql.


hi to all, I've mysql server in centos 6 and client in centos 7. I" am building a small project and I want to iterate in table as output of a sql query in mysql server. Now when i iterate a complete table and after that 10-15 lines it gives segmentation fault. here is mine program and output :-
Code:
void product::showAllProduct()
{
    int times;
    char choose;
    clrscr();
    drawrect();
    int l = 5;
    int qstate;
    int i = 0;
    int r = 0;
    gotoxy(15, 5);
    char ch ;
    string strQuery;
    cout << "Welcome To Electronic Store";

    gotoxy(15, 6);
    cout << "Show All Items Menu\n";

    do
    {
        gotoxy(2, 7);
        cout << "Enter P -> producname | Stock -> S |\n Rate -> R | ProductID -> I : ";
        ch = cin.get();

        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        if(toupper(ch) == 'P')
        {
            strQuery = "select *from tableProductRecords order by productname asc;";
        }
        else if(toupper(ch)  == 'S')
        {
            strQuery = "select *from tableProductRecords order by Stock asc;";
        }
        else if(toupper(ch) == 'R')
        {
            strQuery = "select *from tableProductRecords order by Rate asc;";
        }
        else if( toupper(ch) == 'I')
        {
            strQuery = "select *from tableProductRecords order by ProductId asc;";
        }
        mysql = Conn::connection();
        qstate = mysql_query(mysql, strQuery.c_str());
        if(!qstate)
        {
            res = mysql_store_result(mysql);
            i = process_result_set(mysql, res, 10, &times, totallen);
        }
        else
        {
            cout << " error : " << "  "<< mysql_error(mysql) << "  " << endl;
        }

        sql = "select sum(Rate) from tableProductRecords";
        mysql->reconnect = true;
        mysql = Conn::connection();
        qstate = mysql_query(mysql, sql.c_str());

        if(!qstate)
        {
            res = mysql_store_result(mysql);
            if((row = mysql_fetch_row(res)) != nullptr)
            {
                r = atoi(row[0]);
            }
            gotoxy(2, 9);
            cout << "Grand Total of Stock : "<< r;
        }
        gotoxy(29, 8);
        cin.get();
    }while(int(ch) != 10);
}
how to get rid of this segmentation fault error.
Attached Thumbnails
Click image for larger version

Name:	table iteration in c++.png
Views:	20
Size:	156.9 KB
ID:	35614  
 
Old 02-14-2021, 01:40 AM   #2
tinfoil3d
Member
 
Registered: Apr 2020
Location: Japan/RJCC
Distribution: debian, lfs, whatever else i need in qemu
Posts: 268

Rep: Reputation: 75
Man, there are so many languages out there, why do people have to shoot their feet off in 2021 with C and C++? Leave it to hardware and firmware designers, please.
 
Old 02-14-2021, 09:16 AM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
You need more null-pointer checks.

One specific example is that you’re not checking if “res” is null.

Last edited by dugan; 02-14-2021 at 10:12 AM.
 
Old 02-14-2021, 11:50 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by rahulvishwakarma View Post
hi to all, I've mysql server in centos 6 and client in centos 7. I" am building a small project and I want to iterate in table as output of a sql query in mysql server. Now when i iterate a complete table and after that 10-15 lines it gives segmentation fault. here is mine program and output :-
Code:
void product::showAllProduct()
{
    int times;
    char choose;
    clrscr();
    drawrect();
    int l = 5;
    int qstate;
    int i = 0;
    int r = 0;
    gotoxy(15, 5);
    char ch ;
    string strQuery;
    cout << "Welcome To Electronic Store";

    gotoxy(15, 6);
    cout << "Show All Items Menu\n";

    do
    {
        gotoxy(2, 7);
        cout << "Enter P -> producname | Stock -> S |\n Rate -> R | ProductID -> I : ";
        ch = cin.get();

        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        if(toupper(ch) == 'P')
        {
            strQuery = "select *from tableProductRecords order by productname asc;";
        }
        else if(toupper(ch)  == 'S')
        {
            strQuery = "select *from tableProductRecords order by Stock asc;";
        }
        else if(toupper(ch) == 'R')
        {
            strQuery = "select *from tableProductRecords order by Rate asc;";
        }
        else if( toupper(ch) == 'I')
        {
            strQuery = "select *from tableProductRecords order by ProductId asc;";
        }
        mysql = Conn::connection();
        qstate = mysql_query(mysql, strQuery.c_str());
        if(!qstate)
        {
            res = mysql_store_result(mysql);
            i = process_result_set(mysql, res, 10, &times, totallen);
        }
        else
        {
            cout << " error : " << "  "<< mysql_error(mysql) << "  " << endl;
        }

        sql = "select sum(Rate) from tableProductRecords";
        mysql->reconnect = true;
        mysql = Conn::connection();
        qstate = mysql_query(mysql, sql.c_str());

        if(!qstate)
        {
            res = mysql_store_result(mysql);
            if((row = mysql_fetch_row(res)) != nullptr)
            {
                r = atoi(row[0]);
            }
            gotoxy(2, 9);
            cout << "Grand Total of Stock : "<< r;
        }
        gotoxy(29, 8);
        cin.get();
    }while(int(ch) != 10);
}
how to get rid of this segmentation fault error.
AGAIN??
https://www.linuxquestions.org/quest...-c-4175686608/
https://www.linuxquestions.org/quest...es-4175686406/

You keep opening new threads, and never follow up in your others, and they're all incredibly similar to this one. You post incomplete code, and somehow expect us to be able to decipher what you want. You have been programming in C++ for **TEN YEARS** at this point....have you not learned how to use a debugger yet?? You say you're using Codeblocks...fairly sure it has one in there.

AGAIN: Unless you post your COMPLETE CODE, give us an ACTUAL ERROR (past the fairly-useless 'segmentation fault'), there isn't much we can tell you.

Last edited by TB0ne; 02-14-2021 at 11:51 AM.
 
  


Reply



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
LXer: How to Work with Tables (Select, Update, Delete, Create Table, Alter Table, Drop Table) in MySQL LXer Syndicated Linux News 0 02-13-2021 12:10 AM
What are the differences between the normal symbol table, the dynamic symbol table, and the debugging symbol table? watchintv Linux - Software 5 10-22-2016 08:38 AM
[SOLVED] MySQL run SELECT on a table if column A form table 1 equals column A from table 2 robertjinx Linux - Software 1 01-15-2016 10:48 AM
Recursive iteration and file search in java laks Linux - Software 4 04-15-2012 09:39 AM
C++ List iteration and updating list items x_terminat_or_3 Programming 7 03-24-2009 03:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:59 PM.

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