mysql数据库访问编程_C语言实现访问及查询MySQL数据库的方法

本文实例讲述了C语言实现访问及查询MysqL数据库的方法。分享给大家供大家参考,具体如下:

1、添加头文件路径(MysqL安装路径中的include路径)

2、添加库文件(直接从MysqL安装路径中copy libMysqL.lib即可)

3、编程操作数据库

// AccessToMysqL.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include

#include

#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;

}

运行效果:

15150753531.jpg?201804114738

希望本文所述对大家C语言程序设计有所帮助。

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值