NetVision-Technik

NetVision-Technik (http://www.netvision-technik.de/forum/index.php)
-   C / C++ (http://www.netvision-technik.de/forum/forumdisplay.php?f=59)
-   -   mysql Beispiel (http://www.netvision-technik.de/forum/showthread.php?t=2824)

Dangerman 09.02.2009 17:59

mysql Beispiel
 
C++ und mysql Beispiel


PHP-Code:

#include"stdafx.h"
#include<iostream>
#include<winsock.h>
#include<my_global.h>
#include<mysql.h>
#include<string>
#include<stdio.h>
#pragmacomment (lib, "libmysql.lib")
usingnamespace std;
 
int main(int argcchar *argv[]) {
MYSQL *conn;
MYSQL_RES *res NULL;
MYSQL_ROW row;
char auswahl 0;
string url "";
string passwort "";
string username "";
int eingabe 0;
char sqlquery[1000];
char *server "localhost"//host
char *user "root"//user
char *password "pw"//passwort
char *database "irc"//Datenbank
conn mysql_init(NULL);
/* verbinden zur DB */
if (!mysql_real_connect(connserver,
userpassworddatabase0NULL0)) 
{
cout << stderr << " " << mysql_error(conn) << endl;
system ("PAUSE");
exit(
0);
}
else
{
cout << "Verbindung zur Datenbank wurde erfolgreich hergestellt" << endl;
}

cout << "Geben Sie bitte die Nummer ein, was sie machen moechten\n" << endl;
cout << "1: Neuen Datensatz eintragen" << endl;
cout << "2: Passwoerter anschauen" << endl;
cin >> eingabe;
switch (
eingabe)
{
case 
1:

system ("CLS");

cout << "Geben Sie die URL oder den Programmnamen ein" << endl;
cin >> url;
cout << "Und nun bitte das Passwort" << endl;
cin >> passwort;
cout << "Geben Sie nun den Benutzernamen ein" << endl;
cin >> username;
cout << "Jetzt die ID, in der es in der Datenbank gespeichert werden soll" << endl;
cin >> auswahl;

/* den Inhalt der SQL Query in eine Variable packen*/
 
sprintf(sqlquery"INSERT INTO fragen (id, url, passwort, username) VALUES (%c, '%s', '%s', '%s')",auswahlurl.c_str(), passwort.c_str(), username.c_str());


/* das SQL query senden*/
if (mysql_query(connsqlquery)) 
{
cout << stderr << mysql_error(conn) << endl;
system ("PAUSE");
exit(
0);
}
break;

case 
2:

system ("CLS");
cout << "Geben Sie die ID an" << endl;
cin >> auswahl;
/*die Auswahl wie die id des Datensatzes ist die ausgegeben werden soll*/
sprintf(sqlquery"SELECT * FROM fragen WHERE id = %c",auswahl);
if (
mysql_query(connsqlquery)) 
{
cout << stderr << mysql_error(conn) << endl;
system ("PAUSE");
exit(
0);
}
res mysql_use_result(conn);
row mysql_fetch_row(res);
cout << "ID : " << row[0] << endl;
cout << "URL : " << row[1] << endl;
cout << "Passwort : " << row[2] << endl;
cout << "Username : " << row[3] << endl;
break;
}

/* Speicher freigeben und Verbindung trennen */
mysql_free_result(res);
mysql_close(conn);
system ("PAUSE");
return 
0;




Alle Zeitangaben in WEZ +1. Es ist jetzt 18:20 Uhr.

Powered by vBulletin® Version 3.8.9 (Deutsch)
Copyright ©2000 - 2024, vBulletin Solutions, Inc.