现在的位置: 首页 > 综合 > 正文

C++操作MySQL,有用的朋友顶下,辛苦的原创啊. – 天下 – C++博客

2012年04月22日 ⁄ 综合 ⁄ 共 1209字 ⁄ 字号 评论关闭

C++操作MySQL,有用的朋友顶下,辛苦的原创啊. - 天下 - C++博客

向google大神搜 :
mysql-connector

http://www.mysql.com/products/connector/

这些就是mysql所谓的连接器吧.
一路向下看到:
C++ Wrapper for MySQL C API (MySQL++) Download
http://gna.org/projects/mysqlpp/
下载手册慢慢慢慢看吧你.

1. 先到http://tangentsoft.net/mysql++/ 下载mysql++源码.
2. 将mysql++的VS2008的PRO打开后编译成msyqlpp.lib,mysqlpp.dll等几个动态或静态库。
需要注意的是mysql的头文件及相关库文件需指定地方或加到VS工具的option->vc directories中.
3.请把相应的DEBUG及RELEASE版本的DLL及LIB放到相应的目录.否则调试报异常.
#include <afxwin.h>
#include 
<iostream>
#include 
"lib/mysql++.h"
using namespace std;
int main(){
    
char name[50];
    mysqlpp::Connection conn(
false);
    
if (conn.connect("test","192.168.0.175","root","aaaaaa") ) {
        mysqlpp::Query SetCharacterSetQuery 
= conn.query("SET names 'utf8'");
        SetCharacterSetQuery.exec(); 
//注意这里还要SetCharacterSetQuery.exec() ,和C语言的API不同.
        mysqlpp::Query query = conn.query("select * from doc_threads");
        mysqlpp::StoreQueryResult res 
= query.store();
        
if (res) {
            cout 
<< res[0]["id"<< ' ' << CW2A(CA2W(res[0]["name"],CP_UTF8),CP_ACP) << ' ' <<endl;
            cout 
<< res[1]["id"<< ' ' << CW2A(CA2W(res[1]["name"],CP_UTF8),CP_ACP) << ' ' <<endl;
        }
    }
    conn.disconnect();
    
return 0;
}

抱歉!评论已关闭.