Hello everyone, here I bring you an example of how a connection between C ++ and MySQL would be in GNU / Linux, of course this is just a basic example. First of all to compile I am going to use G ++ and they must have the packages installed libmysql ++ y libmysql ++ - dev . Well here is the example:
#include #include #include using namespace std; #define SERVER "HOST" #define USER "USR" #define PASSWORD "PSWD" #define DATABASE "example" int main () {MYSQL * connect; connect = mysql_init (NULL); if (! connect) {cout << "MySQL Initialization failed"; return 1; } connect = mysql_real_connect (connect, "HOST", "USER", "PASWD", "DATA BASE", 0, NULL, 0); if (connect) {cout << "connection Succeeded \ n"; } else {cout << "connection failed \ n"; } MYSQL_RES * res_set; MYSQL_ROW row; mysql_query (connect, "select * from data;"); unsigned int i = 0; res_set = mysql_store_result (connect); unsigned int numrows = mysql_num_rows (res_set); cout << endl; cout << "\ t -------------------------------------------- ------------------------- \ t "<< endl; while (((row = mysql_fetch_row (res_set))! = NULL)) {// cout << "% s \ n", row [i]! = NULL? row [i]: "NULL"; cout << "\ t | \ t" << row [i] << "\ t | \ t" << row [i + 1] << "\ t | \ t" << row [i + 2] << "\ t | \ t" << endl; cout << "\ t -------------------------------------------- ------------------------- \ t "<< endl; } mysql_close (connect); return 0; }
Well, if you don't know how to compile in G ++ this is an example:
g ++ -o main main.cpp -L / usr / include / mysql -lmysqlclient -I / usr / include / mysql
Then you run it and it would look like this:
------------------------------------- | 1 | Carmen | 46 | ------------------------------------- | 2 | Juan | 56 | --------------------------------------
Note: you can also compile it using MAKEFILE METHOD.