#include
#include
#include
#pragma comment(lib,"libmySQL.lib")
#pragma comment(lib,"wsock32.lib")
#include
#define MAX_COLUMN_LEN 32
void main(){
MYSQL my_connection; //定义数据库连接的句柄,它被用于几乎所有的MySQL函数
MYSQL_RES *result; //查询结果集,结构类型
MYSQL_ROW sql_row; //存放一行查询结果的字符串数组
MYSQL_FIELD *fd; //包含字段信息的结构
char column[MAX_COLUMN_LEN][MAX_COLUMN_LEN];
int res;
mysql_init(&my_connection);
if(mysql_real_connect(&my_connection,"192.168.3.3"
,"root","tdrtdr","gateway",3306,NULL,0))
{
perror("connect");
res=mysql_query(&my_connection,"select * from fcardlist");//查询
if(!res)
{
result=mysql_store_result(&my_connection);//保存查询到的数据到result
if(result)
{
int i,j;
printf("the result number is %lu\n ",(unsigned long)mysql_num_rows(result));
for(i=0;fd=mysql_fetch_field(result);i++)//获取列名
{
memset(column[i],0,sizeof(column[i]));
strcpy(column[i],fd->name);
}
j=mysql_num_fields(result);
for(i=0;i
{
printf("%s\t",column[i]);
}
printf("\n");
while(sql_row=mysql_fetch_row(result))//获取具体的数据
{
for(i=0;i
{
printf("%s\t",sql_row[i]);
}
printf("\n");
}
}
}
else
{
perror("select");
}
}
else
{
perror("connect:error");
}
mysql_free_result(result);//释放结果资源
mysql_close(&my_connection);//断开连接
}