-
Notifications
You must be signed in to change notification settings - Fork 2
/
connect_mysql.c
60 lines (55 loc) · 2 KB
/
connect_mysql.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <mysql/mysql.h>
#include <stdio.h>
/*
* Date: 2018-04-24
*
* Description: Sample program to connect MySQL database, execute some query and
* parse result. Column values are as per select statement like
* row[0] corresponds to value of first column selected, row[1] is
* value of second column in select statement.
*
* Compile this with mysqlclient library as:
* gcc -g connect_mysql.c -lmysqlclient
*/
int main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "abc123"; /* set me first */
char *database = "test";
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 status")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* Sample output of show status command.
mysql> show status;
+-----------------------------------------------+---------------------------+
Variable_name | Value |
+-----------------------------------------------+---------------------------+
| Aborted_clients | 8 |
| Aborted_connects | 1 |
| Binlog_cache_disk_use | 0 |
+-----------------------------------------------+---------------------------+
*
* This query gives me 2 columns so we are reading first column value
* (Variable names) on 0th index and Value from 1st index.
*/
/* Query output */
printf("MySQL status variables:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("Variable name: %s\t\t\t\tStatus: %s\n", row[0], row[1]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}