Advertisement
Guest User

Untitled

a guest
May 6th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. /*
  2. Simple C program that connects to MySQL Database server
  3. Compile using = gcc -o mysql-output $(mysql_config --cflags) mysql.c $(mysql_config --libs)
  4. Pkg = sudo apt-get install libmysqlclient-dev
  5. */
  6.  
  7. #include <mysql.h>
  8. #include <stdio.h>
  9.  
  10. int main() {
  11. MYSQL *conn;
  12. MYSQL_RES *res;
  13. MYSQL_ROW row;
  14.  
  15. char *server = "localhost";
  16. char *user = "root";
  17. char *password = "root"; /* set me first */
  18. char *database = "mysql";
  19.  
  20. conn = mysql_init(NULL);
  21.  
  22. /* Connect to database */
  23. if (!mysql_real_connect(conn, server,
  24. user, password, database, 0, NULL, 0)) {
  25. fprintf(stderr, "%s\n", mysql_error(conn));
  26. exit(1);
  27. }
  28.  
  29. /* send SQL query */
  30. if (mysql_query(conn, "show tables")) {
  31. fprintf(stderr, "%s\n", mysql_error(conn));
  32. exit(1);
  33. }
  34.  
  35. res = mysql_use_result(conn);
  36.  
  37. /* output table name */
  38. printf("MySQL Tables in mysql database:\n");
  39. while ((row = mysql_fetch_row(res)) != NULL)
  40. printf("%s \n", row[0]);
  41.  
  42. /* close connection */
  43. mysql_free_result(res);
  44. mysql_close(conn);
  45. return (0);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement