您的位置:首页 > 编程语言 > C语言/C++

C++如何使用MySQL数据库

2006-11-20 13:27 351 查看
C++如何使用MySQL数据库

MySQL是个免费的软件,就像地上的沙子一样,想拿就拿。
但刚开始使用它,会遇到很多问题。
我经过了长时间的研究,给初学者写了个简单的教程。希望让其他的中国人少走点弯路。错误之出,请指点。谢谢!
如果喜欢的朋友够多,请让我知道,我会继续写的:
//先不费话,先給段代码:

#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <mysql.h>
#include "winsock2.h"

void main()
{
MYSQL_RES* res;
int i;
MYSQL *myData;
// 初使化
myData = mysql_init((MYSQL*) 0);

// 连接
if (! mysql_real_connect(myData, "localhost", "root", "dbcml", NULL, MYSQL_PORT, NULL, 0))
{
printf("Can't connect to the MySQL server on port %d", MYSQL_PORT);
mysql_close(myData);
return;
}

// 操作
myData->reconnect = 1;
if (mysql_select_db(myData, "aaaaa") < 0)
{
printf("Can't select the /"aaaaa/" database./n");
mysql_close(myData);
return;
}

// 读取
if (!mysql_query(myData, "SELECT * FROM pet"))
{
res = mysql_store_result(myData);
i = (int)mysql_num_rows(res);
printf("Number of recodes found: %d/n", i);
}
else
{
printf("Couldn't execute /"SELECT * FROM pet/" on server./n");
return;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: