Files
postgrachelorcppapplication/project/dbclient.pgc
Benjamin Petsch 2ba7cb25ee added CodeBlocks Project
current state: menu, insert category, show categories
2017-03-18 00:27:05 +01:00

103 lines
2.2 KiB
Plaintext

#include <iostream>
#include <stdlib.h>
using namespace std;
void initdb();
void insertCategory();
void menuInsert();
void select();
void cinClear();
int main()
{
initdb();
char choice;
int exit = 0;
for(;!exit;){
choice = 0;
cout << endl << endl << endl << "Was wollen Sie tun?:" << endl;
cout << "(e): Daten eingeben" << endl;
cout << "(a): Daten anzeigen" << endl;
cout << "" << endl;
cout << "(s): schliessen" << endl;
cin >> choice;
switch(choice){
case 'e':
insertCategory();
break;
case 'a':
select();
break;
case 's':
exit = 1;
break;
}
}
}
void initdb(){
EXEC SQL CONNECT TO "CSDB@lamp.wlan.hwr-berlin.de" USER gr2 IDENTIFIED BY "2Au-R2n-cQN-uuG";
cout << "state: " << sqlca.sqlstate << endl;
EXEC SQL COMMIT;
}
void menuInsert(){
insertCategory();
}
void insertCategory(){
EXEC SQL BEGIN DECLARE SECTION;
char eingna[51];
EXEC SQL END DECLARE SECTION;
cout << "Eingabe Name: ";
cin >> eingna;
if (!cin) cinClear();
if (eingna)
{
EXEC SQL INSERT INTO gr2.test(name) VALUES (:eingna);
if (!strcmp(sqlca.sqlstate, "23505")) cout << "GIBT'S SCHON" << endl;
cout << "state: " << sqlca.sqlstate << endl;
EXEC SQL COMMIT;
}
}
void select(){
//Die Ausgabe funktioniert nur dann korrekt, wenn die ids eindeutig sind!!!
EXEC SQL BEGIN DECLARE SECTION;
int id;
char name[51];
EXEC SQL END DECLARE SECTION;
cout << endl << "Ausgabe Tabelle: " << endl;
EXEC SQL DECLARE xxx CURSOR FOR SELECT "ID",name FROM gr2.test ORDER BY "ID";
EXEC SQL OPEN xxx;
int residl;
residl = id;
EXEC SQL FETCH FIRST FROM xxx INTO :id, :name;
if(!strcmp(sqlca.sqlstate, "00000")) cout << id << " " << name << endl;
else if(!strcmp(sqlca.sqlstate, "02000")) cout << "Die Tabelle ist leer!";
while(id != residl)
{
residl = id;
EXEC SQL FETCH NEXT FROM xxx INTO :id, :name;
if(!strcmp(sqlca.sqlstate, "00000")) cout << id << " " << name << endl;
}
EXEC SQL COMMIT;
}
void cinClear() {
char Muell;
cin.clear();
do {
cin.get(Muell);
} while (Muell != '\n');
}