[c_mysql]在freebsd下用c语言连接mysql的具体步骤。

jackxiang 2007-10-12 11:44 | |
如有困难参考:

http://www.toplee.com/blog/329.html
http://tieba.baidu.com/f?kz=206696384
http://oss.lzu.edu.cn/blog/article.php?tid_45.html


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>  
#include "/usr/local/mysql/include/mysql/mysql.h"
int main(void)
{
char mysqlServer[20] = "10.88.15.114";
char query[300];
MYSQL myData;
MYSQL_RES *res;
MYSQL_FIELD *fd;
MYSQL_ROW row;
int rowCount = 0;
int colCount = 0;
int i, j;

mysql_init( &myData );
if(mysql_real_connect( &myData, mysqlServer, "web", "sinatest", "enterprise",3306,NULL,0))
{
printf("connect mysql error!\n");
return 0;
}

sprintf(query,"select * from domain");
if( mysql_query(&myData, query) != 0 )
{
printf("query error!\n");
return 0;
}
else
{
res = mysql_store_result( &myData );
rowCount = (int) mysql_num_rows( res );
colCount = (int) mysql_num_fields( res );
printf(" result: %d records found\n fields: %d \n", rowCount, colCount);
row = mysql_fetch_row( res );
for(i = 0; i < rowCount; i++)
{
printf(" show: ");
for( j = 0; j < colCount; j++)
{
printf("[ %s ] ", row[j] );
} // end for
printf(" \n ");
} // end for
}

return 0;
}


--------------------------------------------------------------
编译:
gcc -o aaa mysqlconn.c -L /usr/local/mysql/lib/mysql/*.a -lz -lm(混乱哈哈)
注意:
要#include <math.h> 编译的时候要加上-lm.
否则出现:

/usr/local/mysql/lib/mysql/libmysqlclient.a(password.o)(.text+0x308): In function `scramble_323':
: undefined reference to `floor'


连接是这样做的:  

if(mysql_real_connect(conn,host_name,user_name,password,port_num,socket_name,flags)==null)  
  {  
  print_error(conn,"mysql_real_connect()   failed");  
  return(null);  
  }  
  if(db_name   !=null)  
  {  
    if(mysql_select_db(conn,db_name)!=0)\{  
    {  
        print_error(conn,"mysql_select_db()   failed");  

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/695/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2008-10-15 14:19
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]