大佬教程收集整理的这篇文章主要介绍了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)) { 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语言程序设计有所帮助。
以上是大佬教程为你收集整理的C语言实现访问及查询MySQL数据库的方法全部内容,希望文章能够帮你解决C语言实现访问及查询MySQL数据库的方法所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。