libsoci_odbc-达梦数据库demo

38 篇文章 0 订阅
1 篇文章 0 订阅

值得注意的是,安装unixODBC后的配置odbcinst.ini为达梦数据库的动态库,若还是找不到文件可以配置/etc/ld.so.conf.d/odbc.conf,将库目录写进去,然后ldconfig

#include "soci/soci.h"
#include "soci/soci-backend.h"
#include "soci/connection-parameters.h"
#include "soci/mysql/soci-mysql.h"
#include "soci/odbc/soci-odbc.h"

#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
#include <time.h>

static soci::backend_factory const &
getBackEnd(); // 获取backend factory

soci::backend_factory const &backEnd = *soci::factory_mysql();
soci::backend_factory const &backEndodbc = *soci::factory_odbc();
static std::string strdbtype0 = "dameng";

bool connectDB(soci::session **session, std::string dbConnString0)
{
    try
    {
        *session = new soci::session(getBackEnd(), dbConnString0);
    }
    catch (soci::soci_error const &ex)
    {
        std::cout << "数据库连接失败:" << ex.what() << std::endl;
        return false;
    }

    return true;
}

soci::backend_factory const &getBackEnd()
{
    if (strdbtype0 == "mysql")
    {
        return backEnd;
    }
    else if (strdbtype0 == "dameng")
    {
        return backEndodbc;
    }
}

/* compile:  g++ soci_odbc_test.cpp -Wl,--copy-dt-needed-entries,-rpath=./,-lpthread,-ldl -I/usr/local/include -L/usr/local/lib64/ -lsoci_core -lsoci_mysql -lsoci_odbc */
int main(int argc, char *argv[])
{

    std::string DBconnectStr = "DSN=DM;Uid=yourUid;Pwd=yourPassWord;Server=127.0.0.1;TCP_PORT=5236;";
    if (argc > 1)
    {
        std::cout << "input url:" << argv[1] << std::endl;
        DBconnectStr = argv[1];
    }
    soci::session *session = nullptr;
    time_t now = time(NULL);

    std::map<std::string, std::vector<std::string>>::iterator iter;

    connectDB(&session, DBconnectStr);

    if (session)
    {
        try
        {
            session->begin();
            session->commit();
            session->begin();
            std::string update_device_m = "update device set stat = 0";
            *session << update_device_m, now;
            session->commit();

            soci::row myrow;
            std::string select_device_m = "select * from danwei ";
            soci::statement st = (session->prepare << select_device_m, soci::into(myrow));
            st.execute();
            while (st.fetch())
            {
                std::cout << myrow.get<std::string>("name") << std::endl;
                std::cout << myrow.get<std::string>("number") << std::endl;
            }
        }
        catch (soci::soci_error &ex)
        {
            session->close();
            return -1;
        }
    }

    if (session)
    {
        try
        {
            std::string insert= "insert into data(name,age) value(\"bob\",50);";
            *session << insert, now;
            session->commit();
        }
        catch (soci::soci_error &ex)
        {
            session->close();
            return -1;
        }
    }

    std::cout << "execute done" << std::endl;
    session->close();
}

其它问题:

Error connecting to database: [unixODBC][Driver Manager]Data source name not found and no default driver specified (SQL state IM002)

需要设置环境变量:

export ODBCINI=/usr/local/etc/odbc.ini
export ODBCSYSINI=/usr/local/etc

配置文件如下:

[root@localhost dujn]# cd /usr/local/etc/
[root@localhost etc]# ls
ODBCDataSources  odbc.ini  odbcinst.ini
[root@localhost etc]# cat odbc.ini
[DM]
Description = DM ODBC DSND
Driver=DM8 ODBC DRIVER
SERVER=127.0.0.1
UID=yourUID
PWD=yourPassword
TCP_PORT=5236

[root@localhost etc]# cat odbcinst.ini
[DM8 ODBC DRIVER]
Description=ODBC DRIVER FOR DB8
Driver=/your/app/path/libdodbc.so

[root@localhost etc]#

*总结

建议自己单独程序单独配置,将编译生成的配置文件复制到自己目录,配置为自己目录的动态库

#ls
drwxr-sr-x 2 root root   6 13 17:54 ODBCDataSources
-rw-r--r-- 1 root root 109 16 18:01 odbc.ini
-rw-r--r-- 1 root root  88 16 18:02 odbcinst.ini

#cat etc/odbc.ini
[DM]
Description = DM ODBC DSND
Driver=DM8 ODBC DRIVER
SERVER=127.0.0.1
UID=yourUID
PWD=yourPassword
TCP_PORT=5236

#cat etc/odbcinst.ini
[DM8 ODBC DRIVER]
Description=ODBC DRIVER FOR DB8
Driver=/your/app/path/libdodbc.so

# 导出环境变量,建议同一个脚本内导出、运行,这样就不会影响/被影响
# export ODBCINI=/your/app/path/etc/odbc.ini
# export ODBCSYSINI=/your/app/path/etc
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值