c语言访问mysql数据库方法

参考范例:

···

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<mysql.h>
#define MAX_COLUMN_LEN 32
int main(int argc , char *argv[])
{
MYSQL db;
MYSQL_RES *res;
MYSQL_ROW row;

//初始化数据库
mysql_init(&db);
//连接数据库
if(mysql_real_connect(&db,"127.0.0.1","root","huawei","information_schema",0,NULL,0))
{
printf("connect!!!\n");
}

//查询
if (mysql_real_query(&db, "select * from CHARACTER_SETS", (unsigned long)strlen("select * from CHARACTER_SETS")))
{
printf("mysql_real_query failed\n");
return 0;
}

// 存储结果集
res = mysql_store_result(&db);
if (NULL == res)
{
printf("mysql_store_result failed\n");
return 0;
}

// 重复读取行,并输出第一个字段的值,直到row为NULL
while (row = mysql_fetch_row(res))
{
printf("%s %s %s %s\n",row[0], row[1], row[2], row[3]);
}

// 释放结果集
mysql_free_result(res);
// 关闭Mysql连接
mysql_close(&db);


return 0;
}

···

posted @ 2019-09-08 18:37  龙之吻hm  阅读(1427)  评论(0编辑  收藏  举报