C语⾔实现访问及查询MySQL数据库的⽅法本⽂实例讲述了C语⾔实现访问及查询MySQL数据库的⽅法。分享给⼤家供⼤家参考,具体如下:
1、添加头⽂件路径(MySQL安装路径中的include路径)
2、添加库⽂件(直接从MySQL安装路径中copy libmysql.lib即可)
3、编程操作数据库
代码
// AccessToMySQL.cpp : 定义控制台应⽤程序的⼊⼝点。
//
#include "stdafx.h"
#include <Windows.h>
#include <mysql.h>
#pragma comment(lib,"libmysql.lib")
MYSQL mysql;
MYSQL_RES* result;
MYSQL_ROW row;
int main(void)
{
//init the mysql parameter
mysql_init(&mysql);
//connect the database
if(!mysql_real_connect(&mysql,"127.0.0.1","root","111","mytest",3306,NULL,0))
{
printf(mysql_error(&mysql));
printf("\nCannot access to the database\n");
system("pause");
exit(-1);
}
//construct the query SQL statements
char* sql="select * from student where name='";
char dest[100]={""};
strcat(dest,sql);
printf("Please enter the student name:");
char name[10]={""};
gets(name);
strcat(dest,name);
strcat(dest,"'");
//excute the SQL statements
if(mysql_query(&mysql,dest))
access转mysql教程视频{
printf("Cannot access the database with excuting \"%s\".",dest);
system("pause");
exit(-1);
}
//deal with the result
result=mysql_store_result(&mysql);
if(mysql_num_rows(result))
{
while((row=mysql_fetch_row(result)))
{
printf("%s\t%s\t%s\n",row[0],row[1],row[2]);
}
}
//release the resource
mysql_free_result(result);
mysql_close(&mysql);
system("pause");
return 0;
}
运⾏效果:
希望本⽂所述对⼤家C语⾔程序设计有所帮助。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。