23,110
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include "mysql.h"
#include <string.h>
void main()
{
char sql[]="select * from user where id=5";
int t;
MYSQL *conn=mysql_init(NULL);
printf("%s\n",mysql_error(conn));
MYSQL_RES *res; //这个结构代表返回行的一个查询结果集
MYSQL_ROW row; //一个行数据的类型安全(type-safe)的表示
if(!mysql_real_connect(conn,"localhost","root","admin","test", 0, NULL, 0) )
{
printf("连接数据库失败!\n%s\n",mysql_error(conn));
}
t=mysql_real_query(conn,sql,(unsigned int)strlen(sql));
if(t)
{
printf("执行出现异常:%s\n",mysql_error(conn));
}
res=mysql_store_result(conn);
while(row=mysql_fetch_row(res))
{
for(t=0;t<mysql_num_fields(res);t++)
{
printf("%s\t",row[t]);
}
}
mysql_free_result(res);//释放结果集使用的内存。
mysql_close(conn);
}