how to iterate table in C++ and Mysql

hi to all, I've centos 6 server and client as centos 7 and in that codeblocks 20.
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 :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

unsigned long product::displayProduct(string header)
{
    unsigned long l = 0, l2 = 0;

    int times;
    int intch;
    int length = 0;
    string prdin = "";
    string prd2 = "";
    string sql;
    string strbillno;
    int x1 = 24;
    unsigned long  prdid;
    int lines;
    char ch, escpch;
    bool flag = false;

    struct winsize w;


    mysql = Conn::connection();
    mysql->reconnect = true;
    clrscr();
    prdin.clear();
    do
    {
        ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
        clrscr();
        drawrect();

        mysql = Conn::connection();

        get->printMenu(productRecords, header);
        gotoxy(2, 5);
        cout << "Enter Product Name : ";
        gotoxy(x1 + prdin.length(), 5);
        gotoxy(x1, 5);
        ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
        for(int i = x1; i < w.ws_col-1; i++)
        {
            cout << " ";
        }
        gotoxy(x1, 5);
        cout << prdin;
        char ch = getc->getche();
        intch = int(ch);
        string prdsearch;
        if(intch == 27)
        {
            return 0;
        }
        if( intch == 127 && prdin.length() > 0)
        {
            gotoxy(x1, 5);
            for(int x = x1; x < w.ws_col - 1; x++)
            {
                cout << " ";
            }

            prdin = prdin.substr(0, prdin.length() -1);
            gotoxy(x1, 5);
            cout << prdin;
        }
        else if(intch == 10 && prdin.length() == 0)
        {
            prdid = 0;
            return prdid;
        }
        else if (intch == 9)
        {
            sql = "select productid from tableProductRecords where productname like '"+prdin+"%' order by productid asc;";
            qstate = mysql_query(mysql, sql.c_str());

            if(!qstate)
            {
                ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
                clrscr(2, 10, w.ws_col-1, w.ws_row-1);
                res = mysql_store_result(mysql);
                prdids.clear();
                while((row = mysql_fetch_row(res)) != nullptr)
                {
                    prdids.push_back(atoi(row[0]));
                }

                iter = prdids.begin();
                while((int(escpch = getc->getch())) == 9 && iter <= prdids.end())
                {
                    int i = 0;
                    strprdid = std::to_string(*iter++);
                    gotoxy(55, 40);
                    cout << "*iter  = " << *iter;
                    cin.get();
                    sql = "select productname, stock, rate, productid from tableProductRecords where productname like '"+prdin+"%';";
                    mysql = Conn::connection();
                    qstate = mysql_query(mysql, sql.c_str());
                    if(!qstate)
                    {
                        ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
                        clrscr(1, 10, w.ws_col, w.ws_row);
                        drawrect();
                        res = mysql_store_result(mysql);
                        sqlp->process_result_set(mysql, res, 10, &times, totallen);
                        length = *totallen;
                    }
                    mysql = Conn::connection();
                    sql = "select productname, stock, rate, productId  from tableProductRecords where productid = '"+strprdid+"';";
                    qstate = mysql_query(mysql, sql.c_str());
                    if(!qstate )
                    {
                        res = mysql_store_result(mysql);
                        if((row = mysql_fetch_row(res)) != nullptr)
                        {
                            gotoxy(x1, 5);
                            for(int y = x1; y < length + 7 ; y++)
                            {
                                cout << " ";
                            }
                            gotoxy(x1, 5);
                            prd2 = row[0];
                            cout << row[0];
                            gotoxy(x1,6);
                            for(int y = x1; y < length + 7; y++)
                            {
                                cout << " ";
                            }
                            gotoxy(x1, 6);
                            cout << atoi(row[1]);


                            gotoxy(x1, 7);
                            for(int y = x1; y < length + 7; y++)
                            {
                                cout << " ";
                            }
                            gotoxy(x1, 7);
                            cout << atoi(row[2]);

                            gotoxy(x1, 8);
                            for(int y = x1; y < length + 7; y++)
                            {
                                cout << " ";
                            }
                            gotoxy(x1, 8);

                            prdid = atol(row[3]);
                            cout << prdid;
                            gotoxy(x1 + prdin.length() , 5);
                        }

                    }
                    else
                    {
                        gotoxy(1, 11);
                        cout << "error : " << mysql_error(mysql);
                        cin.get();
                    }
                    if(iter == prdids.end())
                    {
                        iter = prdids.begin();
                    }
                    gotoxy(x1, 5);
                    for(int itr = x1; itr < length + 7; itr++)
                    {
                        cout << " ";
                    }
                    gotoxy(x1, 5);
                    cout << prd2;
                    gotoxy(2, 9);
                    for(int i = x1; i < length + 7; i++)
                    {
                        cout << " ";
                    }
                    gotoxy(2,9);
                    cout <<"search string : " << prdin;
                    gotoxy(x1, 5);
                }
            }
        }

        else
        {
            prdin += ch;
        }
    }while(intch != 10);

    return prdid;
}

how to get rid of this segmentation fault error.
Last edited on
Topic archived. No new replies allowed.