Advertisement
Guest User

Untitled

a guest
May 14th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <winsock.h>
  6. #include <mysql.h>
  7. #include <sstream>
  8.  
  9. // just going to input the general details and not the port numbers
  10. struct connection_details
  11. {
  12.     char *server;
  13.     char *user;
  14.     char *password;
  15.     char *database;
  16. };
  17.  
  18. struct user {
  19.     int u_id;
  20.     char *u_name;
  21.     char *u_sname;
  22.     int u_active;
  23. };
  24.  
  25. MYSQL* mysql_connection_setup(struct connection_details mysql_details)
  26. {
  27.      // first of all create a mysql instance and initialize the variables within
  28.     MYSQL *connection = mysql_init(NULL);
  29.    
  30.     // connect to the database with the details attached.
  31.     if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) {
  32.       printf("Conection error : %s\n", mysql_error(connection));
  33.       exit(1);
  34.     }
  35.     return connection;
  36. }
  37.  
  38. MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
  39. {
  40.    // send the query to the database
  41.    if (mysql_query(connection, sql_query))
  42.    {
  43.       printf("MySQL query error : %s\n", mysql_error(connection));
  44.       exit(1);
  45.    }
  46.    
  47.    return mysql_use_result(connection);
  48. }
  49.  
  50. int main()
  51. {
  52.   MYSQL *conn;      // the connection
  53.   MYSQL_RES *res;   // the results
  54.   MYSQL_ROW row;    // the results row (line by line)
  55.  
  56.   struct connection_details mysqlD;
  57.   mysqlD.server = "localhost";  // where the mysql database is
  58.   mysqlD.user = "root";     // the root user of mysql  
  59.   mysqlD.password = "data@35"; // the password of the root user in mysql
  60.   mysqlD.database = "cpp_data"; // the databse to pick
  61.  
  62.   // connect to the mysql database
  63.   conn = mysql_connection_setup(mysqlD);
  64.  
  65.   //res = mysql_perform_query(conn, "insert into users values('3','R','S','1')");
  66.   struct user myuser;
  67.   myuser.u_id = 4;
  68.   myuser.u_name = "edo";
  69.   myuser.u_sname = "k";
  70.   myuser.u_active = 1;
  71.  
  72.   std::stringstream temp;
  73.   temp<<"insert into users values('";
  74.   temp<<myuser.u_id;
  75.   temp<<"','";
  76.   temp<<myuser.u_name;
  77.   temp<<"','";
  78.   temp<<myuser.u_sname;
  79.   temp<<"','";
  80.   temp<<myuser.u_active;
  81.   temp<<"')";
  82.  
  83.   //res = mysql_perform_query(conn, (char*)temp.str().c_str());
  84.  
  85.   // assign the results return to the MYSQL_RES pointer
  86.   res = mysql_perform_query(conn, "select * from users");
  87.  
  88.   printf("MySQL Tables in mysql database:\n");
  89.   while ((row = mysql_fetch_row(res)) !=NULL)
  90.       printf("%s\n", row[1]);
  91.  
  92.   /* clean up the database result set */
  93.   mysql_free_result(res);
  94.   /* clean up the database link */
  95.   mysql_close(conn);
  96.  
  97.   system("PAUSE");
  98.   return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement