· 7 years ago · Oct 24, 2018, 05:44 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 "haslo"
18#define b "baza"
19
20float 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 switch(opcja)
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 case 1:
63
64 char Imie[40], Nazw[40];
65 int Numer, Wplat;
66 cout << "Podaj Numer" << endl;
67 cin >> Numer;
68 cout << "Podaj Imie" << endl;
69 cin >> Imie;
70 cout << "Podaj Nazwisko " << endl;
71 cin >> Nazw;
72 cout << "Podaj Aktualna Ilosc Wplat" << endl;
73 cin >> Wplat;
74 char buff[256];
75 snprintf(buff, sizeof(buff), "INSERT INTO uczniowie(`Numer`,`Imie`,`Nazwisko`,`WplatyLacznie`) VALUES('%d', '%s', '%s', '%d');", Numer, Imie, Nazw, Wplat);
76 mysql_query(&baza, buff);
77 system("pause");
78 system("cls");
79 goto step2;
80 break;
81 case 2:
82 int Numer;
83 cout << "Chcesz Usunac Uzytkownika" << endl;
84 cout << "Podaj Jego Numer z Dziennika:" << endl;
85 cin >> Numer;
86 char buff[256];
87 snprintf(buff, sizeof(buff), "DELETE FROM uczniowie WHERE Numer='%d'", Numer);
88 mysql_query(&baza, buff);
89 system("pause");
90 system("cls");
91 goto step2;
92 break ;
93 case 3:
94 int numer, wplata, nowystan;
95 char buff[256];
96 MYSQL_RES *queryid;
97 MYSQL_ROW wiersz;
98 cout << "Podaj Numer Osoby Ktorej Chcesz Dodac Wplate : " << endl;
99 cin >> numer;
100 snprintf(buff, sizeof(buff), "SELECT Wplatylacznie FROM uczniowie WHERE Numer='%d'", numer);
101 mysql_query(&baza, buff);
102 queryid = mysql_store_result(&baza);
103 wiersz = mysql_fetch_row(queryid);
104 cout << "Wplaty Aktuanle: " << wiersz[0] << endl;
105 int aktstan = atoi(wiersz[0]);
106 cout << "Ile Pieniedzy Chcesz Dodac ? " << endl;
107 cin >> wplata;
108 nowystan = aktstan + wplata;
109 cout << "Aktualny Stan Wplat Tego Ucznia to :" << nowystan << endl;
110 snprintf(buff, sizeof(buff), "UPDATE uczniowie SET WplatyLacznie=%d WHERE Numer=%d ;", nowystan, numer);
111 mysql_query(&baza, buff);
112 system("pause");
113 s_stan = s_stan + wplata;
114 s_wplaty = s_wplaty + wplata;
115 snprintf(buff, sizeof(buff), "UPDATE skarbnik SET StanKonta= %d , WplatyLacznie=%d WHERE ID=1;", s_stan, s_wplaty);
116 mysql_query(&baza, buff);
117
118
119
120 system("pause");
121 system("cls");
122 goto step2;
123 break;
124 case 4:
125 cout << "5" << endl;
126 cout << "Aktualne Statystyki To :" << endl;
127 cout << "Stan Konta : " << s_stan << endl;
128 cout << "Wplaty Lacznie: " << s_wplaty << endl;
129 cout << "Aktualna Ilosc Wydatkow: " << s_wydatki << endl;
130
131 system("pause");
132 system("cls");
133 goto step2;
134 break;
135 case 6:
136 cout << "6" << endl;
137 int wplata;
138 cout << "Ile Wydatkow Chcesz Dodac ?? " << endl;
139 cin >> wplata;
140 char buff[256];
141 s_wydatki = s_wydatki + wplata;
142 snprintf(buff, sizeof(buff), "UPDATE skarbnik SET Wydatki = %d WHERE ID=1;", s_wydatki);
143 mysql_query(&baza, buff);
144 system("pause");
145 system("cls");
146 goto step2;
147 break;
148 case 7:
149 int wplata;
150 cout << "7" << endl;
151 cout << "Ile masz Aktualnie Pieniedzy? " << endl;
152 cin >> wplata;
153 char buff[256];
154 snprintf(buff, sizeof(buff), "UPDATE skarbnik SET Stankonta = %d WHERE ID=1;", wplata);
155 mysql_query(&baza, buff);
156 system("pause");
157 system("cls");
158 goto step2;
159 break;
160 }
161}
162 mysql_close(&baza);
163 return 0;
164}
165
166void Pobierz()
167{
168 MYSQL_RES *idZapytania;
169 MYSQL_ROW wiersz;
170
171 mysql_select_db(&baza, "baza");
172 mysql_query(&baza, "SELECT * FROM uczniowie");
173 idZapytania = mysql_store_result(&baza);
174 while ((wiersz = mysql_fetch_row(idZapytania)) != NULL)
175 {
176 for (int i = 0;i < mysql_num_fields(idZapytania); i++)
177 cout << wiersz[i] << " || ";
178 cout << endl;
179 }
180}
181
182void Status()
183{
184 MYSQL_RES *queryid;
185 MYSQL_ROW wiersz;
186 mysql_query(&baza, "SELECT * FROM skarbnik");
187 queryid = mysql_store_result(&baza);
188 wiersz = mysql_fetch_row(queryid);
189 s_stan = atoi(wiersz[1]);
190 s_wplaty = atoi(wiersz[2]);
191 s_wydatki = atoi(wiersz[3]);
192
193 cout << s_stan << endl;
194 cout << s_wplaty << endl;
195 cout << s_wydatki << endl;
196}