· 7 years ago · Sep 25, 2018, 08:18 PM
1#include "stdafx.h"
2#include <iostream>
3#include <mysql.h>
4#include <string>
5#include <stdio.h>
6#include <stdlib.h>
7
8using namespace std;
9void Pobierz();
10void Test();
11void Status();
12void bzq();
13MYSQL baza;
14
15#define h "127.0.0.1"
16#define u "root"
17#define pp "10marzec"
18#define b "baza"
19
20int s_stan, s_wydatki, s_wplaty;
21
22int main()
23{
24
25 int opcja;
26 mysql_init(&baza);
27 bzq();
28 if (mysql_real_connect(&baza, h, u, pp, b , 0, NULL, 0))
29 {
30 cout << "Polaczono z Baza Danych" << endl;
31 cout << "Tworzenie Tabeli Uczniow" << endl;
32 mysql_query(&baza, "CREATE TABLE IF NOT EXISTS `uczniowie` (`ID` int(11) NOT NULL AUTO_INCREMENT,`Numer` int(11) DEFAULT NULL,`Imie` varchar(255) DEFAULT NULL,`Nazwisko` varchar(255) DEFAULT NULL,`WplatyLacznie` int(11) DEFAULT NULL,PRIMARY KEY(`ID`)) ENGINE = InnoDB DEFAULT CHARSET = utf8;");
33 mysql_query(&baza, "CREATE TABLE IF NOT EXISTS skarbnik (ID int NOT NULL AUTO_INCREMENT, Stankonta int, WplatyLacznie int , Wydatki int, PRIMARY KEY(`ID`));");
34 }
35 else
36 {
37 printf("Blad %d %s \n", mysql_errno(&baza), mysql_error(&baza));
38 system("pause");
39 system("exit");
40
41 }
42 Status();
43 step2:
44 cout << "Co Chcesz Zrobic?" << endl;
45 cout << "1.Sprawdzic Liste Uczniow" << endl;
46 cout << "2.Dodac Ucznia" << endl;
47 cout << "3.Usunac Ucznia(In Progress)" << endl;
48 cout << "4.Dodac Wplate" << endl;
49 cout << "5.Sprawdzic Stan Konta" << endl;
50 cout << "6.Dodac Wydatki" << endl;
51 cout << "7. Zmien Aktualny Stan Konta" << endl;
52 cin >> opcja;
53 if (opcja == 1)
54 {
55 cout << "In Progress" << endl;
56 cout << "ID || Numer || Imie || Naziwsko || Wplaty ||" << endl;
57 Pobierz();
58 system("Pause");
59 system("cls");
60 goto step2;
61
62 }
63 else if(opcja == 2)
64 {
65
66 char Imie[40], Nazw[40];
67 int Numer, Wplat;
68 cout << "Podaj Numer" << endl;
69 cin >> Numer;
70 cout << "Podaj Imie" << endl;
71 cin >> Imie;
72 cout << "Podaj Nazwisko " << endl;
73 cin >> Nazw;
74 cout << "Podaj Aktualna Ilosc Wplat" << endl;
75 cin >> Wplat;
76 char buff[256];
77 snprintf(buff, sizeof(buff), "INSERT INTO uczniowie(`Numer`,`Imie`,`Nazwisko`,`WplatyLacznie`) VALUES('%d', '%s', '%s', '%d');", Numer, Imie, Nazw, Wplat);
78 mysql_query(&baza, buff);
79 system("pause");
80 system("cls");
81 goto step2;
82 }
83 else if (opcja == 3)
84 {
85 int Numer;
86 cout << "Chcesz Usunac Uzytkownika" << endl;
87 cout << "Podaj Jego Numer z Dziennika:" << endl;
88 cin >> Numer;
89 char buff[256];
90 snprintf(buff, sizeof(buff), "DELETE FROM uczniowie WHERE Numer='%d'", Numer);
91 mysql_query(&baza, buff);
92 system("pause");
93 system("cls");
94 goto step2;
95
96 }
97 else if (opcja == 4)
98 {
99 int numer, wplata, nowystan;
100 char buff[256];
101 MYSQL_RES *queryid;
102 MYSQL_ROW wiersz;
103 cout << "Podaj Numer Osoby Ktorej Chcesz Dodac Wplate : " << endl;
104 cin >> numer;
105 snprintf(buff, sizeof(buff), "SELECT Wplatylacznie FROM uczniowie WHERE Numer='%d'", numer);
106 mysql_query(&baza, buff);
107 queryid = mysql_store_result(&baza);
108 wiersz = mysql_fetch_row(queryid);
109 cout << "Wplaty Aktuanle: " << wiersz[0] << endl;
110 int aktstan = atoi(wiersz[0]);
111 cout << "Ile Pieniedzy Chcesz Dodac ? " << endl;
112 cin >> wplata;
113 nowystan = aktstan + wplata;
114 cout << "Aktualny Stan Wplat Tego Ucznia to :" << nowystan << endl;
115 snprintf(buff, sizeof(buff), "UPDATE uczniowie SET WplatyLacznie=%d WHERE Numer=%d ;", nowystan, numer);
116 mysql_query(&baza, buff);
117 system("pause");
118 s_stan = s_stan + wplata;
119 s_wplaty = s_wplaty + wplata;
120 snprintf(buff, sizeof(buff), "UPDATE skarbnik SET StanKonta= %d , WplatyLacznie=%d WHERE ID=1;", s_stan, s_wplaty);
121 mysql_query(&baza, buff);
122
123
124
125 system("pause");
126 system("cls");
127 goto step2;
128 }
129 else if (opcja == 5)
130 {
131 cout << "5" << endl;
132 cout << "Aktualne Statystyki To :" << endl;
133 cout << "Stan Konta : " << s_stan << endl;
134 cout << "Wplaty Lacznie: " << s_wplaty << endl;
135 cout << "Aktualna Ilosc Wydatkow: " << s_wydatki << endl;
136
137 system("pause");
138 system("cls");
139 goto step2;
140 }
141 else if (opcja == 6)
142 {
143 cout << "6" << endl;
144 int wplata;
145 cout << "Ile Wydatkow Chcesz Dodac ?? " << endl;
146 cin >> wplata;
147 char buff[256];
148 s_wydatki = s_wydatki + wplata;
149 snprintf(buff, sizeof(buff), "UPDATE skarbnik SET Wydatki = %d WHERE ID=1;", s_wydatki);
150 mysql_query(&baza, buff);
151 system("pause");
152 system("cls");
153 goto step2;
154
155 }
156 else if (opcja == 7)
157 {
158 int wplata;
159 cout << "7" << endl;
160 cout << "Ile masz Aktualnie Pieniedzy? " << endl;
161 cin >> wplata;
162 char buff[256];
163 snprintf(buff, sizeof(buff), "UPDATE skarbnik SET Stankonta = %d WHERE ID=1;", wplata);
164 mysql_query(&baza, buff);
165 system("pause");
166 system("cls");
167 goto step2;
168 }
169 mysql_close(&baza);
170 return 0;
171}
172
173void Pobierz()
174{
175 MYSQL_RES *idZapytania;
176 MYSQL_ROW wiersz;
177
178 mysql_select_db(&baza, "baza");
179 mysql_query(&baza, "SELECT * FROM uczniowie");
180 idZapytania = mysql_store_result(&baza);
181 while ((wiersz = mysql_fetch_row(idZapytania)) != NULL)
182 {
183 for (int i = 0;i < mysql_num_fields(idZapytania); i++)
184 cout << wiersz[i] << " || ";
185 cout << endl;
186 }
187}
188
189void Status()
190{
191 MYSQL_RES *queryid;
192 MYSQL_ROW wiersz;
193 mysql_query(&baza, "SELECT * FROM skarbnik");
194 queryid = mysql_store_result(&baza);
195 wiersz = mysql_fetch_row(queryid);
196 s_stan = atoi(wiersz[1]);
197 s_wplaty = atoi(wiersz[2]);
198 s_wydatki = atoi(wiersz[3]);
199
200 cout << s_stan << endl;
201 cout << s_wplaty << endl;
202 cout << s_wydatki << endl;
203}
204
205
206/*void bzq()
207{
208 string x, y, z, a;
209 cout << "Podaj Host" << endl;
210 cin >> x;
211 cout << "Podaj Uzytkownika :" << endl;
212 cin >> y;
213 cout << "Podaj Haslo" << endl;
214 cin >> z;
215 cout << "Podaj Baze " << endl;
216 cin >> a;
217#undef h
218#undef u
219#undef pp
220#undef b
221
222#define h x
223#define u y
224#define pp z
225#define b a
226}*/