insert into mysql c 2b 2b

Solutions on MaxInterview for insert into mysql c 2b 2b by the best coders in the world

showing results for - "insert into mysql c 2b 2b"
Mathias
01 May 2018
1#include <mysql.h>
2#include <iostream>
3#include <iomanip>
4#include <fstream>
5
6using namespace std;
7
8MYSQL *conn, mysql;
9MYSQL_RES *res;
10MYSQL_ROW row;
11
12int query_state;
13
14int main()
15{
16   const char *server="students";
17   const char *user="*****";
18   const char *password="********";
19   const char *database="*****";
20
21   mysql_init(&mysql);
22   conn=mysql_real_connect(&mysql, server, user, password, database, 0, 0, 0);
23   if(conn==NULL)
24   {
25       cout<<mysql_error(&mysql)<<endl<<endl;
26      return 1;
27   }
28   query_state=mysql_query(conn, "select * from Instrument");
29   if(query_state!=0)
30   {
31      cout<<mysql_error(conn)<<endl<<endl;
32      return 1;
33   }
34   res=mysql_store_result(conn);
35   cout<<"MySQL Tables in mysql database."<<endl<<endl;
36   while((row=mysql_fetch_row(res))!=NULL)
37   {
38      cout<<left;
39      cout<<setw(18)<<row[0]
40          <<setw(18)<<row[1]
41          <<setw(18)<<row[2]
42          <<setw(18)<<row[3]<<endl;
43   }
44   cout<<endl<<endl;
45   ifstream infile;
46   infile.open("Instrument.txt");
47   if(infile.fail())
48   {
49      cout<<"ERROR. Could not open file!"<<endl;
50      return 1;
51   }
52   infile.seekg(0, infile.end);
53   int len=infile.tellg();
54   infile.seekg(0, infile.beg);
55   string instnum, insttype, maker, year, plID, name, salary, startdate;
56   string rating;
57   string instrument="INSERT INTO Instrument (InstrumentID, InstrumentType, MakerName, YearMade) VALUES ( '"+instnum+"', '"+insttype+"', '"+maker+"', '"+year+"')";
58   string player="INSERT INTO Player (PlayerID, Name, Salary, StartDate) values (plID, name, salary, startdate)";
59   string plays="INSERT INTO Plays (InstrumentID, PlayerID, Rating) values (instnum, plID, rating)";
60//   mysql_query(conn, "DELETE FROM Instrument WHERE MakerName='maker'");
61   while(infile)
62   {
63      infile>>instnum;
64      cout<<instnum<<endl;
65      infile>>insttype;
66      cout<<insttype<<endl;
67      infile>>maker;
68      cout<<maker<<endl;
69      infile>>year;
70      cout<<year<<endl;
71      query_state=mysql_query(conn, instrument.c_str());
72   }
73
74   if(query_state!=0)
75   {
76      cout<<mysql_error(conn)<<endl<<endl;
77      return 1;
78   }
79   cout<<endl<<endl;
80
81   mysql_free_result(res);
82   mysql_close(conn);
83
84   return 0;
85
86
87