1 | #include "DataAccess.h"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 | #include <sstream>
|
---|
5 |
|
---|
6 | using namespace std;
|
---|
7 |
|
---|
8 | DataAccess::DataAccess()
|
---|
9 | {
|
---|
10 | mysql_init(&mysql);
|
---|
11 | connection = mysql_real_connect(&mysql, "localhost", "pythonAdmin", "pyMaster09*", "pythondb", 0, 0, 0);
|
---|
12 |
|
---|
13 | if (connection == NULL) {
|
---|
14 | cout << mysql_error(&mysql) << endl;
|
---|
15 | }else
|
---|
16 | cout << "Connection successful" << endl;
|
---|
17 | }
|
---|
18 |
|
---|
19 | DataAccess::~DataAccess()
|
---|
20 | {
|
---|
21 | mysql_close(connection);
|
---|
22 | mysql_close(&mysql);
|
---|
23 | }
|
---|
24 |
|
---|
25 | int DataAccess::insertPlayer(string username, string password)
|
---|
26 | {
|
---|
27 | ostringstream oss;
|
---|
28 |
|
---|
29 | oss << "'" << username << "', '" << password << "'";
|
---|
30 |
|
---|
31 | return insert("users", "name, password", oss.str());
|
---|
32 | }
|
---|
33 |
|
---|
34 | Player *DataAccess::getPlayer(string username)
|
---|
35 | {
|
---|
36 | MYSQL_RES *result;
|
---|
37 | MYSQL_ROW row;
|
---|
38 | Player *p;
|
---|
39 | ostringstream oss;
|
---|
40 |
|
---|
41 | oss << "name='" << username << "'";
|
---|
42 |
|
---|
43 | result = select("users", oss.str().c_str());
|
---|
44 |
|
---|
45 | if (result == NULL) {
|
---|
46 | cout << mysql_error(connection) << endl;
|
---|
47 | return NULL;
|
---|
48 | }
|
---|
49 |
|
---|
50 | if ( ( row = mysql_fetch_row(result)) != NULL )
|
---|
51 | p = new Player(string(row[1]), string(row[2]));
|
---|
52 | else
|
---|
53 | p = NULL;
|
---|
54 |
|
---|
55 | mysql_free_result(result);
|
---|
56 |
|
---|
57 | return p;
|
---|
58 | }
|
---|
59 |
|
---|
60 | int DataAccess::printPlayers()
|
---|
61 | {
|
---|
62 | MYSQL_RES *result;
|
---|
63 | MYSQL_ROW row;
|
---|
64 | ostringstream oss;
|
---|
65 |
|
---|
66 | result = select("users", "");
|
---|
67 |
|
---|
68 | if (result == NULL) {
|
---|
69 | cout << mysql_error(connection) << endl;
|
---|
70 | return 1;
|
---|
71 | }
|
---|
72 |
|
---|
73 | while ( ( row = mysql_fetch_row(result)) != NULL ) {
|
---|
74 | cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
|
---|
75 | }
|
---|
76 |
|
---|
77 | mysql_free_result(result);
|
---|
78 |
|
---|
79 | return 0;
|
---|
80 | }
|
---|
81 |
|
---|
82 | int DataAccess::insert(string table, string rows, string values)
|
---|
83 | {
|
---|
84 | int query_state;
|
---|
85 | ostringstream oss;
|
---|
86 |
|
---|
87 | oss << "INSERT into " << table << " (" << rows << ") VALUES (" << values << ")";
|
---|
88 | cout << "query: " << oss.str() << endl;
|
---|
89 |
|
---|
90 | query_state = mysql_query(connection, oss.str().c_str());
|
---|
91 |
|
---|
92 | if (query_state != 0) {
|
---|
93 | cout << mysql_error(connection) << endl;
|
---|
94 | return 1;
|
---|
95 | }
|
---|
96 |
|
---|
97 | return 0;
|
---|
98 | }
|
---|
99 |
|
---|
100 | MYSQL_RES *DataAccess::select(string table, string filter)
|
---|
101 | {
|
---|
102 | MYSQL_RES *result;
|
---|
103 | int query_state;
|
---|
104 | ostringstream oss;
|
---|
105 |
|
---|
106 | oss << "SELECT * FROM " << table;
|
---|
107 | if (!filter.empty())
|
---|
108 | oss << " WHERE " << filter;
|
---|
109 |
|
---|
110 | query_state = mysql_query(connection, oss.str().c_str());
|
---|
111 |
|
---|
112 | if (query_state != 0) {
|
---|
113 | cout << mysql_error(connection) << endl;
|
---|
114 | return NULL;
|
---|
115 | }
|
---|
116 |
|
---|
117 | return mysql_store_result(connection);
|
---|
118 | }
|
---|