C 读取 MySQL数据库
写过php的朋友一定使用过MySQL,这是我最喜欢的数据库了,这里好处就不说了。看完下面的代码,相信您就已经掌握了写MySQL程序的基本知识了。
第一步:需要做一些VS的配置包含相应的include 和 lib(在mySQL目录下面找)
第二步:链接需要添加 libmysql.lib
第三步:需要建一张mySQL的表。
(这里数据库是Gamer,表名gamerdata,里面有很多的数据自己添加,任您喜好。因为使用SQL语句所以无所谓。)
1
#include <windows.h>//用这个方便,包含winsock.h所以包含winsock也可以
2
#include <iostream>
3
#include <mysql.h>
4
using namespace std;
5
int main( int argc, char * argv[] )
6
{
7
MYSQL mysql;//这类型别错了,具体看文档
8
MYSQL *pdata;
9
MYSQL_RES *result;
10
MYSQL_ROW row;
11
char sqlcommand[80];
12
mysql_init(&mysql);//一个初始化
13
mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"Gamer");//控制链接行为
14
pdata=mysql_real_connect(&mysql,"localhost","root","123456","gamer",0,NULL,0);//数据库链接
15
if(pdata)
16
{
17
cout<<"connect successful!!"<<endl;
18
}
19
if(mysql_select_db(pdata,"gamerdata")) //然后选择表
20
{
21
cout<<"select_db successful!!"<<endl;
22
}
23
sprintf(sqlcommand,"select * from gamerdata");
24
mysql_query(pdata,sqlcommand);
25
result = mysql_use_result(pdata);//获取记录集
26
row = mysql_fetch_row(result);//按数组方式把数据放到row中,当前行的
27
//看所有行加个循环就好,游标自己会跑,不劳大家费心。
28
cout<<row[1];//可以输出了,大功告成
29
mysql_close(pdata);//关闭数据库
30
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)