编译和连接程序
MySQL中有一个特殊的脚本,叫做mysql_config. 它会为你编译MySQL客户端,并连接到MySQL服务器提供有用的信息.你需要使用下面两个选项.
1. --libs 选项 - 连接MySQL客户端函数库所需要的库和选项.
$ mysql_config --libs
2. --cflags 选项 - 使用必要的include文件的选项等等.
$ mysql_config --cflags
你需要将上面两个选项加入到对源文件的编译命令中. 所以,要编译上面的程序,要使用下面的命令:
$ g++ -o output-file $(mysql_config --cflags) test.c $(mysql_config --libs)
执行编译后的程序:
$ ./output.file
#include <mysql.h> #include <stdlib.h> #include <stdio.h> static char *server_args[] = { "this_program", /* this string is not used */ "--datadir=.", "--key_buffer_size=32M" }; static char *server_groups[] = { "embedded", "server", "this_program_SERVER", (char *)NULL }; int main(void) { if (mysql_server_init(sizeof(server_args) / sizeof(char *), server_args, server_groups)) exit(1); MYSQL *conn; MYSQL_RES *res; MYSQL_ROW row; char *server = "localhost"; char *user = "admin"; char *password = "metrics"; /* 此处改成你的密码 */ 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 tables")) { fprintf(stderr, "%s/n", mysql_error(conn)); exit(1); } res = mysql_use_result(conn); //utput table name */ printf("MySQL Tables in mysql database:/n"); while ((row = mysql_fetch_row(res)) != NULL) printf("%s /n", row[0]); printf ("Number of rows: %lu/n", (unsigned long) mysql_num_rows(res)); MYSQL_FIELD *field; while ((field = mysql_fetch_field(res))) { printf("field name %s/n", field->name); } int num_fields = mysql_num_fields(res); for (int i = 0; i < num_fields; i++) { field = mysql_fetch_field_direct(res, i); printf("Field %u is %s/n", i, field->name); } if (!mysql_set_character_set(conn, "utf8")) { MY_CHARSET_INFO cs; mysql_get_character_set_info(conn, &cs); printf("character set information:/n"); printf("character set name: %s/n", cs.name); printf("collation name: %s/n", cs.csname); printf("comment: %s/n", cs.comment); printf("directory: %s/n", cs.dir); printf("multi byte character min. length: %d/n", cs.mbminlen); printf("multi byte character max. length: %d/n", cs.mbmaxlen); } /* close connection */ mysql_free_result(res); mysql_close(conn); /* Use any MySQL API functions here */ mysql_server_end(); return EXIT_SUCCESS; }
运行结果如下: