bianbianbiandaren 2017-02-04 12:27 采纳率: 0%
浏览 1169

Linux C 调用mysql API 无法通过编译 怎么回事

 11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include <mysql/mysql.h>
16 
17 MYSQL *g_conn; // mysql 连接
18  MYSQL_RES *g_res; // mysql 记录集
19  MYSQL_ROW g_row; // 字符串数组,mysql 记录行
20  
21 #define MAX_BUF_SIZE 1024 // 缓冲区最大字节数
22 
23 const char *g_host_name = "localhost";
24 const char *g_user_name = "root";
25 const char *g_password = "root";
26 const char *g_db_name = "test";
27 const unsigned int g_db_port = 3306;
28 
29 void print_mysql_error(const char *msg) { // 打印最后一次错误
30     if (msg)
31         printf("%s: %s\n", msg, mysql_error(g_conn));
32     else
33         puts(mysql_error(g_conn));
34 }
35 
36 int executesql(const char * sql) {
37     /*query the database according the sql*/
38     if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
39         return -1; // 表示失败
40 
41     return 0; // 成功执行
42 }
43 
44 
45 int init_mysql() { // 初始化连接
46     // init the database connection
47     g_conn = mysql_init(NULL);
48 
49     /* connect the database */
50     if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败
51         return -1;
52 
53     // 是否连接已经可用
54     if (executesql("set names utf8")) // 如果失败
55         return -1;
56 
57     return 0; // 返回成功
58 }
59 
60 
61 int main(void) {
62     puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
63 
64     if (init_mysql());
65         print_mysql_error(NULL);
66 
67     char sql[MAX_BUF_SIZE];
68     sprintf(sql, "INSERT INTO `test`(`name`) VALUES('testname')");
69 
70     if (executesql(sql))
71         print_mysql_error(NULL);
72 
73     if (executesql("SELECT * FROM `test`")) // 句末没有分号
74         print_mysql_error(NULL);
75 
76     g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
77 
78     int iNum_rows = mysql_num_rows(g_res); // 得到记录的行数
79     int iNum_fields = mysql_num_fields(g_res); // 得到记录的列数
80 
81     printf("共%d个记录,每个记录%d字段\n", iNum_rows, iNum_fields);
82 
83     puts("id\tname\n");
84 
85     while ((g_row=mysql_fetch_row(g_res))) // 打印结果集
86         printf("%s\t%s\n", g_row[0], g_row[1]); // 第一,第二字段
87 
88     mysql_free_result(g_res); // 释放结果集
89 
90     mysql_close(g_conn); // 关闭链接
91 
92     return EXIT_SUCCESS;
93 }

[root@localhost opt]# gcc mysql.c -lmysqlclient
mysql.c:4:25: 错误:mysql/mysql.h:没有那个文件或目录
mysql.c:6: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
mysql.c:7: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
mysql.c:8: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘g_row’
mysql.c: 在函数‘print_mysql_error’中:
mysql.c:21: 错误:‘g_conn’未声明(在此函数内第一次使用)
mysql.c:21: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其
mysql.c:21: 错误:所在的函数内也只报告一次。)
mysql.c: 在函数‘executesql’中:
mysql.c:29: 错误:‘g_conn’未声明(在此函数内第一次使用)
mysql.c: 在函数‘init_mysql’中:
mysql.c:39: 错误:‘g_conn’未声明(在此函数内第一次使用)
mysql.c: 在函数‘main’中:
mysql.c:63: 错误:‘g_res’未声明(在此函数内第一次使用)
mysql.c:63: 错误:‘g_conn’未声明(在此函数内第一次使用)
mysql.c:72: 错误:‘g_row’未声明(在此函数内第一次使用)

  • 写回答

1条回答

  • devmiao 2017-02-04 15:39
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题