-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_query_from_file.cc
96 lines (82 loc) · 2.27 KB
/
send_query_from_file.cc
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
#include <mysql.h>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
const char* user = "maxuser";
const char* password = "maxpwd";
const char* db = "test";
const char* host = "127.0.0.1";
int port = 4006;
#define USE_ASYNC 0
int main(int argc, char** argv)
{
MYSQL* c = mysql_init(nullptr);
if (!mysql_real_connect(c, host, user, password, db, port, nullptr, 0))
{
std::cout << "Connect: " << mysql_error(c) << std::endl;
}
else
{
int results = 0;
std::ifstream file(argv[1]);
std::string line;
std::vector<std::string> queries;
while (std::getline(file, line))
{
queries.push_back(line);
results++;
}
for (const auto& q : queries)
{
if (mysql_send_query(c, q.c_str(), q.length()))
{
std::cout << "mysql_send_query: " << mysql_error(c) << std::endl;
return 1;
}
}
for (const auto& q : queries)
{
if (mysql_read_query_result(c))
{
std::cout << "mysql_read_query_result: " << mysql_error(c) << std::endl;
return 1;
}
else if (auto res = mysql_use_result(c))
{
while (auto row = mysql_fetch_row(res))
{
for (int i = 0; i < mysql_num_fields(res); i++)
{
std::cout << row[i] << "\t";
}
std::cout << std::endl;
}
mysql_free_result(res);
}
}
/*
mysql_close(c);
c = mysql_init(nullptr);
if (!mysql_real_connect(c, host, user, password, db, port, nullptr, 0))
{
std::cout << "Connect: " << mysql_error(c) << std::endl;
return 1;
}
for (const auto& q : queries)
{
if (mysql_query(c, q.c_str()))
{
std::cout << "mysql_real_query: " << mysql_error(c) << std::endl;
return 1;
}
else
{
mysql_free_result(mysql_use_result(c));
}
}
*/
}
mysql_close(c);
return 0;
}