【转】在ubuntu下 mysql.h的使用

此文章主要向大家描述的是安装MySQL数据库中获得 MySQL.h 建立C接口的实际操作流程,首先我们是从安装MySQL数据库开始的,其中涉及相关的实际应用代码的描述,下面就是文章的具体内容描述。
先安装MySQL
代码:
1. sudo apt-get install MySQL-server MySQL-client 
再装开发包
代码:
1. sudo apt-get install libMySQLclient15-dev 
安装MySQL数据库完以后,C代码里添加头文件
代码:
1. #include <mysql.h> 
编译方法:
代码:
1. gcc $(mysql_config --cflags) xxx.c -o xxx $(mysql_config --libs) 
可以用以下代码测试一下
代码:
1. /* Simple C program that connects to MySQL Database server*/  
2. #include <mysql.h> 
3. #include <stdio.h> 
4. main() {  
5. MYSQL *conn;  
6. MYSQL_RES *res;  
7. MYSQL_ROW row;  
8. char *server = "localhost";  
9. char *user = "root";  
10. char *password = "";   
此处改成你的密码
1. char *database = "mysql";  
2. conn = mysql_init(NULL);  
3. /* Connect to database */  
4. if (!mysql_real_connect(conn, server,  
5. user, password, database, 0, NULL, 0)) {  
6. fprintf(stderr, "%s\n", mysql_error(conn));  
7. exit(1);  
8. }  
9. /* send SQL query */  
10. if (mysql_query(conn, "show tables")) {  
11. fprintf(stderr, "%s\n", mysql_error(conn));  
12. exit(1);  
13. }  
14. res = mysql_use_result(conn);  
15. /* output table name */  
16. printf("MySQL Tables in mysql database:\n");  
17. while ((row = mysql_fetch_row(res)) != NULL)  
18. printf("%s \n", row[0]);  
19. /* close connection */  
20. mysql_free_result(res);  
21. mysql_close(conn);  
22. }  
会输出现有数据库和表内容。以上的相关内容就是对安装MySQL数据库获得 MySQL.h 建立C接口的介绍,望你能有所收获。

 

源地址:http://entryblog.blog.163.com/blog/static/20963103320127211262881/

posted on 2013-02-25 21:55  TheKingOfKingFish  阅读(1341)  评论(0)    收藏  举报

导航