示例
编译和连接程序
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
Cpp 代码
1. #include<mysql.h>
2. #include<stdlib.h>
3. #include<stdio.h>
4. staticchar*server_args[]=
5. {
6. "this_program",/*thisstringisnotused*/
7. "--datadir=.",
8. "--key_buffer_size=32M"
9. };
10. staticchar*server_groups[]=
11. {
12. "embedded",
13. "server",
14. "this_program_SERVER",
15. (char*)NULL
16. };
17. intmain(void)
18. {
19. if(mysql_server_init(sizeof(server_args)/sizeof(char*),
20. server_args,server_groups))
21. exit(1);
22. MYSQL*conn;
23. MYSQL_RES*res;
24. MYSQL_ROWrow;
25. char*server="localhost";
26. char*user="admin";
27. char*password="metrics";/*此处改成你的密码 */
28. char*database="test";
29. conn=mysql_init(NULL);
30. /*Connecttodatabase*/
评论8