forked from magellannh/RPI_Efergy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysql-c-api.c
executable file
·46 lines (37 loc) · 1002 Bytes
/
mysql-c-api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
See url for more info:
http://www.cyberciti.biz/tips/linux-unix-connect-mysql-c-api-program.html
*/
#include <mysql.h>
#include <stdio.h>
int main(void) {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
/* Change me */
char *server = "localhost";
char *user = "root";
char *password = "*****";
char *database = "efergymonitor";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
return 0;
}