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;

}

2a7948c0d42087dfdd46cebfb054f2c5.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值