Commit | Line | Data |
---|---|---|
44ac5003 JB |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include <mysql/mysql.h> | |
4 | ||
5 | //il faut : libmysqlclient-dev | |
6 | ||
7 | //compilation : | |
8 | // gcc testsqlc.c -o testsqlc -I/usr/include/mysql -lmysqlclient -L/usr/lib64/mysql | |
9 | ||
10 | //execution : | |
11 | //./testsqlc | |
12 | ||
13 | ||
14 | int main(void) { | |
15 | MYSQL *conn; | |
16 | MYSQL_RES *res; | |
17 | MYSQL_ROW row; | |
18 | MYSQL_RES *result; | |
19 | ||
20 | char *server = ""; | |
21 | char *user = ""; | |
22 | char *password = ""; | |
23 | char *database = ""; | |
24 | ||
25 | conn = mysql_init(NULL); | |
26 | ||
27 | /* Connect to database */ | |
28 | if (!mysql_real_connect(conn, server, | |
29 | user, password, database, 0, NULL, 0)) { | |
30 | fprintf(stderr, "%s\n", mysql_error(conn)); | |
31 | exit(1); | |
32 | } | |
33 | ||
34 | /* send SQL query */ | |
35 | if (mysql_query(conn, "show tables")) { | |
36 | fprintf(stderr, "%s\n", mysql_error(conn)); | |
37 | exit(1); | |
38 | } | |
39 | ||
40 | res = mysql_use_result(conn); | |
41 | ||
42 | /* output table name */ | |
43 | printf("MySQL Tables in mysql database:\n"); | |
44 | while ((row = mysql_fetch_row(res)) != NULL) | |
45 | printf("%s \n", row[0]); | |
46 | ||
47 | /* query */ | |
48 | if (mysql_query(conn, "SELECT * FROM Usine")) | |
49 | { | |
50 | fprintf(stderr, "%s\n", mysql_error(conn)); | |
51 | exit(1); | |
52 | } | |
53 | ||
54 | result = mysql_store_result(conn); | |
55 | ||
56 | if (result == NULL) | |
57 | { | |
58 | fprintf(stderr, "%s\n", mysql_error(conn)); | |
59 | exit(1); | |
60 | } | |
61 | ||
62 | int num_fields = mysql_num_fields(result); | |
63 | int i; | |
64 | ||
65 | printf("\nTable Usine :\n"); | |
66 | printf("NU\tNomU\tVille\n"); | |
67 | ||
68 | while ((row = mysql_fetch_row(result))) | |
69 | { | |
70 | for(i = 0; i < num_fields; i++) | |
71 | { | |
72 | printf("%s\t", row[i] ? row[i] : "NULL"); | |
73 | } | |
74 | printf("\n"); | |
75 | } | |
76 | ||
77 | /* close connection */ | |
78 | mysql_free_result(res); | |
79 | mysql_close(conn); | |
80 | ||
81 | return 0; | |
82 | } | |
83 |