From: Jérôme Benoit Date: Mon, 18 Dec 2017 18:53:57 +0000 (+0100) Subject: Make the MySQL example code snippet compile. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=3dc7dfe648c664f59dfce9d3e5e39720ab4ae6ae;p=TD_BDD.git Make the MySQL example code snippet compile. Signed-off-by: Jérôme Benoit --- 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; } -