CPP (jeb C ++) + MySQL

MySQL

Sveiki visi, šeit es jums parādīšu piemēru, kā savienojums starp C ++ un MySQL būtu GNU / Linux, protams, tas ir tikai pamatpiemērs. Pirmkārt, lai apkopotu, es izmantoju G ++, un viņiem ir jābūt instalētām pakotnēm libmysql ++ y libmysql ++ - dev . Nu, šeit ir piemērs:

# iekļaut # iekļaut # iekļaut izmantojot nosaukumvietu std; #define SERVER "HOST" #define USER "USR" #define PASSWORD "PSWD" #define DATABASE "example" int main () {MYSQL * connect; savienot = mysql_init (NULL); if (! connect) {cout << "MySQL inicializēšana neizdevās"; atgriezties 1; } connect = mysql_real_connect (savienot, "HOST", "USER", "PASWD", "DATA BASE", 0, NULL, 0); if (savienot) {cout << "savienojums izdevies \ n"; } else {cout << "savienojums neizdevās \ n"; } MYSQL_RES * res_set; MYSQL_ROW rinda; mysql_query (savienot, "izvēlēties * no datiem;"); neparakstīts int i = 0; res_set = mysql_store_result (savienot); neparakstītas int numrows = mysql_num_rows (res_set); cout << endl; cout << "\ t -------------------------------------------- ------------------------- \ t "<< endl; while (((rinda = mysql_fetch_row (res_set))! = NULL)) {// cout << "% s \ n", rinda [i]! = NULL? rinda [i]: "NULL"; cout << "\ t | \ t" << rinda [i] << "\ t | \ t" << rinda [i + 1] << "\ t | \ t" << rinda [i + 2] << "\ t | \ t" << endl; cout << "\ t -------------------------------------------- ------------------------- \ t "<< endl; } mysql_close (savienot); atgriešanās 0; }

Nu, ja jūs nezināt, kā apkopot G ++, tas ir piemērs:

g ++ -o main main.cpp -L / usr / include / mysql -lmysqlclient -I / usr / include / mysql

Tad jūs to palaižat, un tas izskatās šādi:

------------------------------------- | 1 | Karmena | 46 | ------------------------------------- | 2 | Huans | 56 | --------------------------------------

Piezīme: jūs varat to arī apkopot, izmantojot MAKEFILE METHOD.


Pievienot kā vēlamo avotu pakalpojumā Google