C++ save data in Mysql table

Hello,

I try to save C++ data in Mysql table, but unsuccessfuly.
May be someone can help me?
Thanks for helping,
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
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <winsock.h>
#include <mysql.h>
using namespace std;
 int worker;
class Workers 
{
public:
 int number;
 string name;
 
};
class Tax:public Workers
{   
double min;
 
double netto;
public:
void print();
 void input();
 void save();   
Tax()
{               
min=70;
}      
      };  
void Tax::input()
 {
     while (! cin.good()) 
        {
              cin.clear();
             cin.ignore (256, '\n');
             cout<<"Please enter number:"<<endl;
             }
  cout<<"\nPlease enter: ";
  cin>>number;
  cout<<"\nEnter name: ";
  cin>>name;
}
 
 
 
void Tax::save()
{
  MYSQL *conn;  
  MYSQL_RES *result;  
  MYSQL_ROW row;  
 int num_fields;  
 int i;  
 conn = mysql_init(NULL);  
  if(!mysql_real_connect(conn, "localhost", "root", "", "trafic", 0, NULL, 0)){  
    printf("cannot connect");  
}  
    else{  
   for (int i=0;i<worker;i++)
  mysql_query(conn, "INSERT INTO data('id','data') VALUES ("+count[i].number+","+count[i].name+")");  
 
 
    mysql_close(conn);  
    } 
}
 
void Tax::print()
{  
    cout<<endl;
    cout.width(10);
    cout.setf(ios::left, ios::adjustfield);
    cout<<number;
    cout.width(12);
    cout<<name;
   
}
int main ()
{
 
Tax count[10];
int select;
bool close=false;
//system ("pause");
while(!close)
{
cout<<"\nYour choice:\n";
cin>>select;
switch(select)
{
case 1:
     cout<<"\nEnter count: ";
cin>>worker;
    for (int i=0;i<worker;i++) 
count[i].input();  
     break;
case 2:
    cout<<"\n"<<setw(5)<<"Number"
<<setw(9)<<"Name";
  
for (int i=0;i<worker;i++)
count[i].print();
    
     char base;
   cout<< " Save data? Press Y";
   cin>>base;
  if (base=='Y'){
        for (int i=0;i<worker;i++) 
count[i].save();              
         
}        
              
     break;
case 3:
     close=true;
     break;
     default:
cout<<"\nYour choice:\n";
     break;    
        }    
}
//system ("pause");
return 0;
}
Last edited on
Topic archived. No new replies allowed.