· 9 years ago · Nov 13, 2016, 05:24 PM
1// Konsolowa1.cpp : Defines the entry point for the console application.
2//
3
4#include "stdafx.h"
5#include <iostream>
6#include <mysql.h>
7#include <string>
8#include <stdio.h>
9#include <stdlib.h>
10
11using namespace std;
12void Pobierz();
13void Test();
14void Status();
15MYSQL baza;
16
17int s_stan, s_wydatki, s_wplaty;
18
19int main()
20{
21 int opcja;
22 mysql_init(&baza);
23 if (mysql_real_connect(&baza, "127.0.0.1", "root", "10marzec", "baza", 0, NULL, 0))
24 {
25 cout << "Polaczono z Baza Danych" << endl;
26 cout << "Tworzenie Tabeli Uczniow" << endl;
27 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;");
28 mysql_query(&baza, "CREATE TABLE IF NOT EXISTS skarbnik (ID int NOT NULL AUTO_INCREMENT, Stankonta int, WplatyLacznie int , Wydatki int, PRIMARY KEY(`ID`));");
29 }
30 else
31 {
32 printf("Blad %d %s \n", mysql_errno(&baza), mysql_error(&baza));
33 system("pause");
34 system("exit");
35
36 }
37 Status();
38 step2:
39 cout << "Co Chcesz Zrobic?" << endl;
40 cout << "1.Sprawdzic Liste Uczniow" << endl;
41 cout << "2.Dodac Ucznia" << endl;
42 cout << "3.Usunac Ucznia(In Progress)" << endl;
43 cout << "4.Dodac Wplate" << endl;
44 cout << "5.Sprawdzic Stan Konta" << endl;
45 cout << "6.Dodac Wydatki" << endl;
46 cout << "7. Zmien Aktualny Stan Konta" << endl;
47 cin >> opcja;
48 if (opcja == 1)
49 {
50 cout << "In Progress" << endl;
51 cout << "ID || Numer || Imie || Naziwsko || Wplaty ||" << endl;
52 Pobierz();
53 system("Pause");
54 system("cls");
55 goto step2;
56
57 }
58 else if(opcja == 2)
59 {
60
61 char Imie[40], Nazw[40];
62 int Numer, Wplat;
63 cout << "Podaj Numer" << endl;
64 cin >> Numer;
65 cout << "Podaj Imie" << endl;
66 cin >> Imie;
67 cout << "Podaj Nazwisko " << endl;
68 cin >> Nazw;
69 cout << "Podaj Aktualna Ilosc Wplat" << endl;
70 cin >> Wplat;
71 char buff[256];
72 snprintf(buff, sizeof(buff), "INSERT INTO uczniowie(`Numer`,`Imie`,`Nazwisko`,`WplatyLacznie`) VALUES('%d', '%s', '%s', '%d');", Numer, Imie, Nazw, Wplat);
73 mysql_query(&baza, buff);
74 system("pause");
75 system("cls");
76 goto step2;
77 }
78 else if (opcja == 3)
79 {
80 int Numer;
81 cout << "Chcesz Usunac Uzytkownika" << endl;
82 cout << "Podaj Jego Numer z Dziennika:" << endl;
83 cin >> Numer;
84 char buff[256];
85 snprintf(buff, sizeof(buff), "DELETE FROM uczniowie WHERE Numer='%d'", Numer);
86 mysql_query(&baza, buff);
87 system("pause");
88 system("cls");
89 goto step2;
90
91 }
92 else if (opcja == 4)
93 {
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 }
124 else if (opcja == 5)
125 {
126 cout << "5" << endl;
127 cout << "Aktualne Statystyki To :" << endl;
128 cout << "Stan Konta : " << s_stan << endl;
129 cout << "Wplaty Lacznie: " << s_wplaty << endl;
130 cout << "Aktualna Ilosc Wydatkow: " << s_wydatki << endl;
131
132 system("pause");
133 system("cls");
134 goto step2;
135 }
136 else if (opcja == 6)
137 {
138 cout << "6" << endl;
139 int wplata;
140 cout << "Ile WydatkowChces Dodac ?? " << endl;
141 cin >> wplata;
142 char buff[256];
143 s_wydatki = s_wydatki + wplata;
144 snprintf(buff, sizeof(buff), "UPDATE skarbnik SET Wydatki = %d WHERE ID=1;", s_wydatki);
145 mysql_query(&baza, buff);
146 system("pause");
147 system("cls");
148 goto step2;
149
150 }
151 else if (opcja == 7)
152 {
153 int wplata;
154 cout << "7" << endl;
155 cout << "Ile masz Aktualnie Pieniedzy? " << endl;
156 cin >> wplata;
157 char buff[256];
158 snprintf(buff, sizeof(buff), "UPDATE skarbnik SET Stankonta = %d WHERE ID=1;", wplata);
159 mysql_query(&baza, buff);
160 system("pause");
161 system("cls");
162 goto step2;
163 }
164 mysql_close(&baza);
165 return 0;
166}
167
168void Pobierz()
169{
170 MYSQL_RES *idZapytania;
171 MYSQL_ROW wiersz;
172
173 mysql_select_db(&baza, "baza");
174 mysql_query(&baza, "SELECT * FROM uczniowie");
175 idZapytania = mysql_store_result(&baza);
176 while ((wiersz = mysql_fetch_row(idZapytania)) != NULL)
177 {
178 for (int i = 0;i < mysql_num_fields(idZapytania); i++)
179 cout << wiersz[i] << " || ";
180 cout << endl;
181 }
182}
183
184void Status()
185{
186 MYSQL_RES *queryid;
187 MYSQL_ROW wiersz;
188 mysql_query(&baza, "SELECT * FROM skarbnik");
189 queryid = mysql_store_result(&baza);
190 wiersz = mysql_fetch_row(queryid);
191 s_stan = atoi(wiersz[1]);
192 s_wplaty = atoi(wiersz[2]);
193 s_wydatki = atoi(wiersz[3]);
194
195 cout << s_stan << endl;
196 cout << s_wplaty << endl;
197 cout << s_wydatki << endl;
198}