Advertisement
Guest User

Untitled

a guest
May 31st, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include <mysql.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. // just going to input the general details and not the port numbers
  6. struct connection_details
  7. {
  8. char *server;
  9. char *user;
  10. char *password;
  11. char *database;
  12. };
  13.  
  14. MYSQL* mysql_connection_setup(struct connection_details mysql_details)
  15. {
  16. // first of all create a mysql instance and initialize the variables within
  17. MYSQL *connection = mysql_init(NULL);
  18.  
  19. // connect to the database with the details attached.
  20. if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) {
  21. printf("Conection error : %s\n", mysql_error(connection));
  22. exit(1);
  23. }
  24. return connection;
  25. }
  26.  
  27. MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
  28. {
  29. // send the query to the database
  30. if (mysql_query(connection, sql_query))
  31. {
  32. printf("MySQL query error : %s\n", mysql_error(connection));
  33. exit(1);
  34. }
  35.  
  36. return mysql_use_result(connection);
  37. }
  38.  
  39. int main()
  40. {
  41. MYSQL *conn; // the connection
  42. MYSQL_RES *res; // the results
  43. MYSQL_ROW row; // the results row (line by line)
  44.  
  45. struct connection_details mysqlD;
  46. mysqlD.server = "........."; // where the mysql database is
  47. mysqlD.user = "........."; // the root user of mysql
  48. mysqlD.password = "........."; // the password of the root user in mysql
  49. mysqlD.database = "........."; // the databse to pick
  50.  
  51. // connect to the mysql database
  52. conn = mysql_connection_setup(mysqlD);
  53.  
  54. // assign the results return to the MYSQL_RES pointer
  55. res = mysql_perform_query(conn, "show tables");
  56.  
  57. printf("MySQL Tables in mysql database:\n");
  58. while ((row = mysql_fetch_row(res)) !=NULL)
  59. printf("%s\n", row[0]);
  60.  
  61. /* clean up the database result set */
  62. mysql_free_result(res);
  63. /* clean up the database link */
  64. mysql_close(conn);
  65.  
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement