CentOS下Connector/C++的使用

前端之家收集整理的这篇文章主要介绍了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),用起来太麻烦,内存管理很复杂。

原文链接:https://www.f2er.com/centos/379201.html

猜你在找的CentOS相关文章