总结
最近由于涉及到用vs2012来连接数据库mysql的问题,特总结这几天所做的工作;
一、查阅资料得知,在vs2012下连接mysql一般有这几种方法: DAO连接,mysql-connecter++连接,mysql自身头文件与库文件链接;
1、DAO连接:没尝试过,不过能连接各种数据库。
2、mysql-connecter连接:在java下只要下载mysql-connecter/J文件,将jar文件导入工程即可,但在c++中,用官方编译好的文件,则在getstring语句下crach,通过google下得知,官网上编译好的文件,与自己vs的编译环境不一样导致的,其解决方案是下载源代码,自行编译;
可参考一下博客:
http://blog.csdn.net/shellching/article/details/8137806
http://blog.sina.com.cn/s/blog_df2bc89a0101kc52.html
http://blog.csdn.net/yunfei_1973/article/details/9230151 虽然按这篇博文编译成功,但还是会出现getString的问题,不明情况
3、mysql自身来连接:不像前面方式那样编写方便。
二、我自己最后在此是用第三种,先完成功能即可。这里要提下的是,我安装在电脑上的mysql是wammpservel集成的,由于没有配置mysql的过程,所以在mysql下是没有libmysql.lib等文件的,我采取的是下载与wammpservel中mysql相同的version,将其中没有的文件拷贝到wammpservel中,即可。
参考了一下博文:
http://blog.csdn.net/hngchiming/article/details/8256587
http://blog.csdn.net/biezhihua/article/details/9499977
一些函数介绍:http://gamba.epm.br/pub/manuais/manual_mysql/chapter4.html
以下为代码示例:
插入一条数据:
#include "stdafx.h"
#include
#include
#include
using namespace std;
//插入一条数据到yuippe中score表中
void insert2table(char* host, char* user, char* pass, char* db)
{
MYSQL my_connection;
int res;
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection, host, user, pass, db, 3306, NULL, 0))
{
printf("Connection success\n");
res = mysql_query(&my_connection,"INSERT INTO score VALUES(4, 808, 'OOXX', 100)");
if (!res)
{
printf("Inserted %lu rows\n", (unsigned long)mysql_affected_rows(&my_connection));
}
else
{
fprintf(stderr, "Insert error %d: %s\n", mysql_errno(&my_connection),
mysql_error(&my_connection));
}
mysql_close(&my_connection);
}else
{
fprintf(stderr, "Connection failed\n");
if (mysql_errno(&my_connection))
{
fprintf(stderr, "Connection error %d: %s\n",
mysql_errno(&my_connection), mysql_error(&my_connection));
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char *host = "localhost";
char *user = "root";
char *pass = "123456";
char *db = "yuippe";
insert2table(host,user,pass,db);
system("pause");
return 0;
}
----------------------------------
查询一个表:
int _tmain(int argc, _TCHAR* argv[])
{
MYSQL *sock;
MYSQL_RES *results;
MYSQL_ROW record;
char *host = "localhost";
char *user = "root";
char *pass = "123456";
char *db = "yuippe";
if(!(sock = mysql_init(0)))
{
cout << "sock" << endl;
}
if(!mysql_real_connect(sock, host, user, pass, db, 3306, NULL, 0))
{
cout << "failed to connect" << endl;
}
mysql_set_character_set(sock, "utf8");
mysql_query(sock, "SELECT * FROM score ");
results = mysql_store_result(sock);
//while (record = mysql_fetch_row(results))
//{
// /*cout << record[0] << "\t";
// cout << record[1] << "\t";
// cout << record[2] << "\t";
// cout << record[3] << endl;*/
// /*int id = *record[0];
// cout << (char)id << "\t";
// int stu_id = *record[1];*/
// char *name = record[2];
// string str = name;
// printf("name: %s\t", name);
//}
while (record = mysql_fetch_row(results))
{
/*cout << record[0] << "\t";
cout << record[1] << "\t";
cout << record[2] << "\t";
cout << record[3] << endl;*/
/*int id = *record[0];
cout << (char)id << "\t";
int stu_id = *record[1];*/
char *name = record[2];
string str = name;
printf("name: %s\t", name);
}
if (results != NULL)
{
mysql_free_result(results);
}
mysql_close(sock);
system("pause");
return 0;
}