免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1121 | 回复: 3
打印 上一主题 下一主题

c或者c++连接数据库请教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-01-14 21:54 |只看该作者 |倒序浏览
有相关的教程或者例子么?
我安装了odbc,自带的例子可以连接上数据库并执行操作成功,但是没有详细的介绍,请问哪位有相关的资料或者说明的,谢谢!
数据库是informix....

论坛徽章:
0
2 [报告]
发表于 2007-01-14 22:20 |只看该作者
这是我以前收藏的一个链接,参考下吧,偶没弄过
http://bbs.chinaunix.net/viewthread.php?tid=286998

论坛徽章:
0
3 [报告]
发表于 2007-01-15 10:06 |只看该作者

  1. #include <unistd.h>;
  2. #include <arpa/inet.h>;
  3. #include <stdio.h>;
  4. #include <stdlib.h>;
  5. #include <string.h>;
  6. #include <sys/types.h>;
  7. #include <sys/socket.h>;
  8. #include <netinet/in.h>;
  9. #include <mysql/mysql.h>;
  10. #include <signal.h>;
  11. #include <errno.h>;
  12. #include <syslog.h>;

  13. MYSQL mysql;
  14. main()
  15. {
  16.         char host[32]="localhost";
  17.         char user[32]="root";
  18.         char passwd[32]="abc";
  19.         char dbname[32]="test";        
  20.         
  21.         if( mysql_init(&mysql) == NULL )
  22.         {
  23.                 syslog(LOG_USER|LOG_INFO,"inital mysql handle error\n");
  24.                 return 1;
  25.         }
  26.         if ( mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0)
  27.                          == NULL )
  28.         {
  29.                 syslog(LOG_USER|LOG_INFO,
  30.                                            "Failed to connect to database: Error: %s\n",
  31.                                            mysql_error(&mysql));
  32.                                
  33.                 return 1;
  34.         }
  35.         else syslog(LOG_USER|LOG_INFO, "connect to database: \n");
  36.         find_ps();        
  37.         db_close();
  38.         return 0;
  39. }
  40. int db_close()
  41. {
  42.         mysql_close(&mysql);
  43.          return 0;
  44. }


  45. int  find_ps ()
  46. {
  47.         MYSQL_ROW m_row;
  48.         MYSQL_RES *m_res;
  49.         char sql[1024],username[32];
  50.         int res=1;
  51.         int *id;
  52.         sprintf(sql,"select id from username");
  53.         if(mysql_query(&mysql,sql) != 0)
  54.         {
  55.                 syslog(LOG_USER|LOG_INFO,
  56.                                            "select ps_info Error: %s\n",
  57.                                            mysql_error(&mysql));
  58.                 return res;
  59.         }
  60.         m_res = mysql_store_result(&mysql);
  61.         if(m_res==NULL)
  62.         {
  63.                 syslog(LOG_USER|LOG_INFO,
  64.                                            "select username Error: %s\n",
  65.                                            mysql_error(&mysql));
  66.                 res = 3;
  67.                 return res;
  68.         }
  69.         if(m_row = mysql_fetch_row(m_res))
  70.         {
  71.                 printf("m_row=%d\n",atoi(m_row[0]));
  72.                 res = 0;               
  73.         }
  74.         mysql_free_result(m_res);
  75.         return res;
复制代码


连MySQL的 你看看吧 呵呵

论坛徽章:
0
4 [报告]
发表于 2007-01-15 15:54 |只看该作者
多谢楼上两位!!!在openlink里面找到了个例子,正在试验中
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP