LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2467|回复: 5

如何在linux下利用c连接mysql并执行查询搜索汇总等?

[复制链接]
发表于 2004-2-16 13:27:00 | 显示全部楼层 |阅读模式

  1. #include <stdlib.h>
  2. #include <stdio.h>

  3. #include "mysql.h"

  4. MYSQL my_connection;
  5. MYSQL_RES *res_ptr;
  6. MYSQL_ROW sqlrow;

  7. int main(int argc, char *argv[]) {
  8.         int res;
  9.         mysql_init(&my_connection);
  10.         if (mysql_real_connect(&my_connection,"localhost","rick","bar", "rick", 0, NULL, 0)) {
  11.                 printf("Connection success\n");
  12.                 res = mysql_query(&my_connection, "SELECT childno, fname,age FROM children WHERE age > 5");
  13.                 if (res) {
  14.                         printf("SELECT error: %s\n", mysql_error(&my_connection));
  15.                 } else {
  16.                         res_ptr = mysql_store_result(&my_connection);
  17.                         if (res_ptr) {
  18.                                 printf("Retrieved %luows\n",(unsigned long)mysql_num_rows(res_ptr));
  19.                                 while ((sqlrow = mysql_fetch_row(res_ptr))) {
  20.                                         printf("Fetched data...\n");
  21.                                 }
  22.                                 if (mysql_errno(&my_connection)) {
  23.                                         fprintf(stderr, "Retrive error: s\n",mysql_error(&my_connection));
  24.                                 }
  25.                         }
  26.                         mysql_free_result(res_ptr);
  27.                 }
  28.                 mysql_close(&my_connection);
  29.         } else {
  30.                 fprintf(stderr, "Connection failed\n");
  31.                 if (mysql_errno(&my_connection)) {
  32.                         fprintf(stderr, "Connection error %d: %s\n",
  33.                         mysql_errno(&my_connection),mysql_error(&my_connection));
  34.                 }
  35.         }

  36.                 return EXIT_SUCCESS;
  37. }
复制代码


这段代码中的第4行  #include "mysql.h"
编译时提示不存在

然后改为           #include "mysql/mysql.h"
编译时提示如下:


  1. /tmp/cccsfBE0.o: In function `main':
  2. /tmp/cccsfBE0.o(.text+0xf): undefined reference to `mysql_init'
  3. /tmp/cccsfBE0.o(.text+0x36): undefined reference to `mysql_real_connect'
  4. /tmp/cccsfBE0.o(.text+0x65): undefined reference to `mysql_query'
  5. /tmp/cccsfBE0.o(.text+0x83): undefined reference to `mysql_error'
  6. /tmp/cccsfBE0.o(.text+0xa9): undefined reference to `mysql_store_result'
  7. /tmp/cccsfBE0.o(.text+0xd1): undefined reference to `mysql_num_rows'
  8. /tmp/cccsfBE0.o(.text+0xf6): undefined reference to `mysql_fetch_row'
  9. /tmp/cccsfBE0.o(.text+0x12d): undefined reference to `mysql_errno'
  10. /tmp/cccsfBE0.o(.text+0x146): undefined reference to `mysql_error'
  11. /tmp/cccsfBE0.o(.text+0x16d): undefined reference to `mysql_free_result'
  12. /tmp/cccsfBE0.o(.text+0x17d): undefined reference to `mysql_close'
  13. /tmp/cccsfBE0.o(.text+0x1a7): undefined reference to `mysql_errno'
  14. /tmp/cccsfBE0.o(.text+0x1bd): undefined reference to `mysql_error'
  15. /tmp/cccsfBE0.o(.text+0x1d0): undefined reference to `mysql_errno'
  16. collect2: ld returned 1 exit status
复制代码


请问如何连接?
mysql_real_connect(&my_connection,"localhost","rick","bar", "rick", 0, NULL, 0)中各个参数是什么意思啊?
发表于 2004-2-17 17:38:29 | 显示全部楼层
我是菜鸟,关注
 楼主| 发表于 2004-2-18 13:22:07 | 显示全部楼层
感谢关注,继续顶
另外编译的时候加上 -L mysqlclient 也不行
发表于 2004-2-18 19:22:18 | 显示全部楼层
-lmysqlclient
发表于 2008-4-3 10:03:18 | 显示全部楼层
试一下  在后面加上  $(mysql_config --cflags) $(mysql_config --libs)

   ^ _^
回复 支持 反对

使用道具 举报

发表于 2008-4-16 11:34:36 | 显示全部楼层
这些代码比较不错,值得收藏,学习一下!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表