动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   PHP → 阅读文章

 c连接mysql的实例

作者来源: 
阅读 数 285 人次 , 2006-4-19 9:55:00 


  
今天给新来的同事做mysql培训,用到的一个例子:
连接数据库test 用户名root密码abc
在里面有一个username的表,describe username结果如下
mysql> describe username;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| username | varchar(32) | | PRI | | |
+----------+-------------+------+-----+---------+-------+

mysql> select * from username;
+------+----------+
| id | username |
+------+----------+
| 1 | sfs |
+------+----------+
1 row in set (0.00 sec)


本文目的查找执行select id from username这个语句并打印结果
代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MYSQL mysql;
main()
{
   char host[32]="localhost";
   char user[32]="root";
   char passwd[32]="abc";
   char dbname[32]="test";   
   
   if( mysql_init(&mysql) == NULL )
   {
      syslog(LOG_USER|LOG_INFO,"inital mysql handle errorn");
      return 1;
   }
   if (mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)
   {
      syslog(LOG_USER|LOG_INFO, "Failed to connect to database: Error: %sn",mysql_error(&mysql));
      return 1;
   }
   else syslog(LOG_USER|LOG_INFO, "connect to database: n");
   find_ps();   
   db_close();
   return 0;
}
int db_close()
{
        mysql_close(&mysql);
         return 0;
}


int  find_ps ()
{
   MYSQL_ROW m_row;
   MYSQL_RES *m_res;
   char sql[1024],username[32];
   int res=1;
   int *id;
   sprintf(sql,"select id from username");
   if(mysql_query(&mysql,sql) != 0)
   {
      syslog(LOG_USER|LOG_INFO, "select ps_info Error: %sn",mysql_error(&mysql));
      return res;
   }
   m_res = mysql_store_result(&mysql);
   if(m_res==NULL)
   {
      syslog(LOG_USER|LOG_INFO, "select username Error: %sn",mysql_error(&mysql));
      res = 3;
      return res;
   }
   if(m_row = mysql_fetch_row(m_res))
   {
      printf("m_row=%dn",atoi(m_row[0]));
      res = 0;      
   }
   mysql_free_result(m_res);
   return res;
}

编译命令
gcc -g connect_db.c -L/usr/lib/mysql -lmysqlclient -lz
注意:
如果/tmp/ccTGmMS21.o: In function `main':
/tmp/ccTGmMS21.o(.text+0x11): undefined reference to `mysql_init'
那么参数增加-L/usr/lib/mysql -lmysqlclient
如果
usr/lib/mysql/libmysqlclient.a(my_compress.o): In function `my_uncompress':
my_compress.o(.text+0xaa): undefined reference to `uncompress'
那么增加-lz参数
  
 本文Tagsmysql  数据库  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:用PHP写的QQ Client,可以登陆、登出、收发消息、添加好友
· 下一篇:mysql 自动备份----aspbiz
· 好东西和大家分享~同时上传100个文件上传的程序~(代码)文件一:fileup.pl
· 一个模仿oso的论坛程序(之三)
· PHP的XML分析函数(转) (介绍这个PHP里的XML分析函数的文章可不太有哦。。 看过这篇就应该清楚点了吧...
· PHP,21世纪的霸主(一)
· PHP5正式版出炉


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077