From 3dc7dfe648c664f59dfce9d3e5e39720ab4ae6ae Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 18 Dec 2017 19:53:57 +0100 Subject: [PATCH] Make the MySQL example code snippet compile. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TD5/C/Makefile | 4 +- TD5/C/exemple_mysql_libmysqlclient.c | 135 +++++++++++++-------------- 2 files changed, 68 insertions(+), 71 deletions(-) diff --git a/TD5/C/Makefile b/TD5/C/Makefile index b5cebf8..04569c3 100644 --- a/TD5/C/Makefile +++ b/TD5/C/Makefile @@ -14,6 +14,8 @@ BINARY_NAME=infovols BUILD_TYPE=debug #BUILD_TYPE=release +LDLIBS=-lmysqlclient +INCLUDES=-I/usr/include/mysql # ==================================== # DO NOT CHANGE STUFF BEYOND THIS LINE @@ -41,7 +43,7 @@ STRIP_FLAG = -s OPTI_FLAG = -O3 endif -CFLAGS := -DDEBUG=$(DEBUG) $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) +CFLAGS := -DDEBUG=$(DEBUG) $(INCLUDES) $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) LDFLAGS := $(LDFLAGS) $(STRIP_FLAG) OBJDIR := $(BUILDDIR)/objs diff --git a/TD5/C/exemple_mysql_libmysqlclient.c b/TD5/C/exemple_mysql_libmysqlclient.c index 88e82da..9f26acc 100644 --- a/TD5/C/exemple_mysql_libmysqlclient.c +++ b/TD5/C/exemple_mysql_libmysqlclient.c @@ -10,74 +10,69 @@ //execution : //./testsqlc - -int main(void) { - MYSQL *conn; - MYSQL_RES *res; - MYSQL_ROW row; - MYSQL_RES *result; - - char *server = ""; - char *user = ""; - char *password = ""; - char *database = ""; - - 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]); - - /* query */ - if (mysql_query(conn, "SELECT * FROM Usine")) - { - fprintf(stderr, "%s\n", mysql_error(conn)); - exit(1); - } - - result = mysql_store_result(conn); - - if (result == NULL) - { - fprintf(stderr, "%s\n", mysql_error(conn)); - exit(1); - } - - int num_fields = mysql_num_fields(result); - int i; - - printf("\nTable Usine :\n"); - printf("NU\tNomU\tVille\n"); - - while ((row = mysql_fetch_row(result))) - { - for(i = 0; i < num_fields; i++) - { - printf("%s\t", row[i] ? row[i] : "NULL"); - } - printf("\n"); - } - - /* close connection */ - mysql_free_result(res); - mysql_close(conn); - - return 0; +int main(void) +{ + MYSQL *conn; + MYSQL_RES *res; + MYSQL_ROW row; + MYSQL_RES *result; + + char *server = ""; + char *user = ""; + char *password = ""; + char *database = ""; + + 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]); + + /* query */ + if (mysql_query(conn, "SELECT * FROM Usine")) { + fprintf(stderr, "%s\n", mysql_error(conn)); + exit(1); + } + + result = mysql_store_result(conn); + + if (result == NULL) { + fprintf(stderr, "%s\n", mysql_error(conn)); + exit(1); + } + + int num_fields = mysql_num_fields(result); + int i; + + printf("\nTable Usine :\n"); + printf("NU\tNomU\tVille\n"); + + while ((row = mysql_fetch_row(result))) { + for (i = 0; i < num_fields; i++) { + printf("%s\t", row[i] ? row[i] : "NULL"); + } + printf("\n"); + } + + /* close connection */ + mysql_free_result(res); + mysql_close(conn); + + return 0; } - -- 2.34.1