CentOS下Connector/C++的使用

安装依赖:

yum install boost
yum install boost-devel
yum install boost-doc
yum install MysqL-devel

下载Connector/C++库:

CentOS6 64位

CentOS6 32位

下载解压得到里面的库文件和头文件


编写测试代码

#include <cstdlib>
#include <iostream>
#include <string.h>
#include "MysqL_connection.h"
#include "MysqL_driver.h"
#include "driver.h"
#include "statement.h"

using namespace std;
using namespace sql;

int main(int argc,char** argv) {

    Driver *driver;
    Connection *conn;
    Statement *state;
    ResultSet *result;
    
    driver = get_driver_instance();
    conn = driver->connect("tcp://localhost:3306","root","123456");
    state = conn->createStatement();
    state->execute("use lpVote1");
    result = state->executeQuery("select * from topic_class");
    
    for(; result->next();){
        cout << result->getString("TopicclassName") << endl;
    }
    
    state->close();
    conn->close();
    delete result;
    delete state;
    delete conn;    

    return 0;
}
编译选项:

-I 指定头文件路径,-L 指定库文件路径,-l 指定库文件,还需要添加`MysqL_config --cflags --libs`,不然报函数未定义的错误

用起来就跟jdbc一样,封装的真好。我曾经研究过OCI(Oracle call Interface),用起来太麻烦,内存管理很复杂。

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native