- 论坛徽章:
- 3
|
我分别在两台机用C进行连接mysql
代码如下tem.c
#include<stdlib.h>;
#include<stdio.h>;
#include <mysql/mysql.h>;
int main()
{
MYSQL mysql;
char MySQL_SQL[1024]={0};
MYSQL_RES *result;
MYSQL_ROW row;
unsigned int num_fields;
unsigned int i;
mysql_init(&mysql);
if (!mysql_real_connect(&mysql,"hostIP","username","password","dbname",3306,NULL,0))
{
printf("Failed to connect to database: Error: %s\n",
mysql_error(&mysql));
return -1;
}
// char MySQL_SQL[1024]={0};
// MYSQL_RES *result;
strncpy(MySQL_SQL,"SELECT * FROM t_mo_RecvCmd\0",sizeof(MySQL_SQL));
if(mysql_real_query(&mysql,MySQL_SQL,strlen(MySQL_SQL)+1)!=0)
{
printf("ʧ°Ü\n" ;
}
result=mysql_store_result(&mysql);
// MYSQL_ROW row;
// unsigned int num_fields;
// unsigned int i;
num_fields = mysql_num_fields(result);
printf("num_fields: %d\n",num_fields);
while ((row = mysql_fetch_row(result)))
{
unsigned long *lengths;
lengths = mysql_fetch_lengths(result);
for(i = 0; i < num_fields; i++)
{
printf("[%d_%s] ", (int) lengths, row ? row : "NULL" ;
}
printf("\n" ;
}
mysql_close(&mysql);
printf("connect database success!\n" ;
return 0;
}
在linux 7.2 上
Linux xxx 2.4.7-10 #1 Thu Sep 6 17:27:27 EDT 2001 i686 unknown
gcc tem.c -lmysqlclient
Mysql 版本(rpm包)
MySQL-shared-4.0.20-0
MySQL-server-4.0.20-0
MySQL-devel-4.0.20-0
MySQL-client-4.0.20-0
可以连上
另一是redhat linux 9.0
gcc tem.c -L/usr/lib/mysql -lmysqlclient
编译成功
但是连接失败
Failed to connect to database: Error: Can't connect to MySQL server on 'hostIP' (111)
系统信息
Linux localhost.localdomain 2.4.18-3 #1 Thu Apr 18 07:37:53 EDT 2002 i686 unknown
mysql 自带的
mysql Ver 11.16 Distrib 3.23.49, for redhat-linux-gnu (i386)
有以下rpm包
MySQL-python-0.9.1-1
perl-DBD-MySQL-1.2219-6 |
|