debian C++ OTL库 用 unixodbc 连接 mysql 小记

这个东东也是折腾了几天,网上很多文章可能已经过时,所以写下不同,以备后用.

参考网址:

http://blog.csdn.net/genganpeng/article/details/7402229

http://blog.csdn.net/liefdiy/article/details/5348583

http://blog.itpub.net/81/viewspace-710064/

http://www.software8.co/wzjs/czxt/4668.html

为什么要用 OTL去连接mysql.

OTL是一个纯C++的通用数据库连接模板库,可以支持各种当下流行的数据库,如Oracle,Sybase, MySQL, PostgreSQL, EnterpriseDB, SQLite,  MS ACCESS, Firebird等等.它是一个跨平台类库,在MS Windows, Linux/Unix/Mac OS X 都可以使用。

可baidu C++ OTL深入了解.

环境

Linux

[email protected]:~# uname -a
Linux debian 3.2.0-4-amd64 #1 SMP Debian 3.2.65-1 x86_64 GNU/Linux

mysql

[email protected]:~# mysql --version
mysql Ver 14.14 Distrib 5.5.43, for debian-linux-gnu (x86_64) using readline 6.2

unixODBC

[email protected]:~# isql --versoin

unixODBC 2.3.2

OTL版本

OTL 4.0.359

下载地址:http://otl.sourceforge.net/

程序安装

1,unixODBC

下载地址:

http://www.unixodbc.org/

1. copy the unixODBC-2.3.2.tar.gz file somewhere you can create files and directories
2. gunzip unixODBC*.tar.gz
3. tar xvf unixODBC*.tar

./configure
make
make install

就OK了

2,mysql

apt-get install mysql-server  mysql-client

还需要安装 mysql的odbc驱动

下载地址:

http://mirrors.sohu.com/mysql/Connector-ODBC/5.3/

如果是新版本改下5.3这个版本号就ok.

我的linux 是x64所以下载

mysql-connector-odbc-5.3.2-linux-glibc2.5-x86-64bit.tar.gz

解压

tar zxvf mysql-connector-odbc-5.3.2-linux-glibc2.5-x86-64bit.tar.gz
cd mysql-connector-odbc-5.3.2-linux-glibc2.5-x86-64bit
./configure
make
make install

配置unixodbc

UNIXODBC 安装好后会有:

/etc/odbcinst.ini 和 /etc/odbc.ini ********但配置这两个文件不会生效,原因我暂时没有找到.请高手指点.

/usr/local/etc/odbcinst.ini 和 /usr/local/etc/odbc.ini 这两个配置文件才会生效.

/usr/local/etc/odbcinst.ini  配置:

[mysqlodbc]      #Section
Driver=/usr/local/lib/libmyodbc5w.so      #libmyodbc5w.so 和libmyodbc5a.so的区别
SETUP=/usr/local/lib/libmyodbc5w.so
UsageCount=1

关于 libmyodbc5w.so 和libmyodbc5a.so的区别 在

http://bugs.mysql.com/bug.php?id=69837

找到了相应的答案.

"

  The odbcinst.ini file shows the driver library name as libmyodbc5.so, which
    is not right for Connector/ODBC 5.2.5. It has to be either libmyodbc5w.so
    (Unicode version) or libmyodbc5a.so (ANSI version). So, which version of
    Connector/ODBC 5.2.5 you used? Was it Unicode (w) or ANSI (a)?

"

  libmyodbc5w.so  是unicode 版本的.
  libmyodbc5a.so 是ansi版本的.

/usr/local/etc/odbc.ini  配置:

[TEST]    #Section
#Driver=/usr/local/lib/libmyodbc5a.so
Description = The Database for mysql
Trace = On
TraceFile = stderr
Driver = mysqlodbc    #odbcinst.ini 中指定的Section
SERVER = localhost     #数据库IP
USER = sa2      #用户名
PASSWORD =  111111      #密码
PORT = 3306        #数据库端口
DATABASE = test1    #要连接的数据库

socket=/var/run/mysqld/mysqld.sock    #虽然这儿指定 mysqld.sock位置,但后面还是会报错,下文有相应解决办法.

charset = UTF8    #指定编码
option = 3

在配置文件里,DSN的名字即为Section的名字。在配置信息中,有一部分配置项是ODBC使用的,另一部分则由驱动程序处理。如果操作完全正确的话,现在ODBC已经成功了。可以试下isql命令操作刚配置的DSN。

可能会报错误:

Can‘t connect to local MySQL server through socket ‘/tmp/mysql.sock‘ (2)

通过查看

  cat /etc/mysql/my.cnf 可以得知:

  socket = /var/run/mysqld/mysqld.sock

  最简单的办法建立软连接.

  命令:ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock

  查看:

  

[email protected]:/home/c2# ls -l /tmp
total 0
lrwxrwxrwx 1 root root 27 Jul 10 00:39 mysql.sock -> /var/run/mysqld/mysqld.sock

  连接成功.

  再运行 命令:

[email protected]:~# isql TEST -v
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> select * from t1;
+-----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Id        | name                                                                                                                                                                                                                                                           | name2                                                                                                                                                                                                                                                          |
+-----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1         | 杩姣                                                                                                                                                                                                                                                         | 灏3                                                                                                                                                                                                                                                           |
| 2         | rq                                                                                                                                                                                                                                                             | fasdf                                                                                                                                                                                                                                                          |
| 3         | 灏寮?                                                                                                                                                                                                                                                        | xiaoZ                                                                                                                                                                                                                                                          |
+-----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
SQLRowCount returns 3
3 rows fetched
SQL> 

t1表的视图:

通过OTL连接mysql

目录内容如下:

otltest.cpp内容如下:

#include <iostream>
using namespace std;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <stdio.h>

// Thefollowing #define is required with MyODBC 5.1 and higher
//#define OTL_ODBC_SELECT_STM_EXECUTE_BEFORE_DESCRIBE  

#define OTL_ODBC // CompileOTL 4.0/ODBC
#define OTL_ODBC_UNIX // uncomment this line if UnixODBC is used
//#define OTL_UNICODE // Compile OTL with Unicode    --->屏蔽 使用时中文显示乱码

#include "otlv4.h" // include the OTL 4.0 header file

otl_connect db; // connect object

void select()
{
    try{
        otl_stream ostream1(500, // buffer size
            "select * from t1 ",
            // SELECT statement
            db // connect object
            );
        // create select stream

        int id;
        unsigned char user[255];
        unsigned  char name[255];

        while(!ostream1.eof())
        { // while not end-of-data
            //ostream1>>id>>user>>name;
            ostream1>>id;
            ostream1>>user;
            ostream1>>name;
            cout<<"id="<<id<<endl;
            cout<<"user="<<user<<endl;
            cout<<"name="<<name<<endl;

        }
    }

    catch(otl_exception& p)
    { // intercept OTL exceptions

        cout<<"otl_exception:"<<endl;
        cerr<<p.msg<<endl; // print out error message
        cerr<<p.stm_text<<endl; // print out SQL that caused the error
        cerr<<p.var_info<<endl; // print out the variable that caused the error
    }

}

int main()
{

    cout<<"hello"<<endl;
    otl_connect::otl_initialize(); // initialize the database API environment
    try{

        db.rlogon("sa2/[email protected]"); // connect to the database 这儿是  用户名/密码@section名 (/usr/local/etc/odbc.ini  的section名)

        select(); // select records from table

    }

    catch(otl_exception& p){ // intercept OTL exceptions
        cerr<<p.msg<<endl; // print out error message
        cerr<<p.stm_text<<endl; // print out SQL that caused the error
        cerr<<p.var_info<<endl; // print out the variable that caused the error
    }

    db.logoff(); // disconnect from the database

    return 0;

}

CMakeLists.txt 内容:

cmake_minimum_required (VERSION 2.6)
project (otltest2)
add_executable(otltest2 otltest.cpp)
target_link_libraries(otltest2 libmyodbc5a.so)   --->


  一定要添加 libmyodbc5a.so  否则会出现 SQLFreeHandle  之类的错误
[email protected]:/home/c2# make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/c2
Scanning dependencies of target otltest2
[100%] Building CXX object CMakeFiles/otltest2.dir/otltest.cpp.o
Linking CXX executable otltest2
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_conn::~otl_conn()‘:
otltest.cpp:(.text._ZN8otl_connD2Ev[_ZN8otl_connD5Ev]+0x63): undefined reference to `SQLFreeHandle‘
otltest.cpp:(.text._ZN8otl_connD2Ev[_ZN8otl_connD5Ev]+0x9b): undefined reference to `SQLFreeHandle‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_conn::rlogon(char const*, int)‘:
otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x532): undefined reference to `SQLAllocHandle‘
otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x588): undefined reference to `SQLSetEnvAttr‘
otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x5df): undefined reference to `SQLAllocHandle‘
otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x64c): undefined reference to `SQLSetConnectAttr‘
otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x67d): undefined reference to `SQLSetConnectAttr‘
otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x6eb): undefined reference to `SQLSetConnectAttr‘
otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x765): undefined reference to `SQLConnect‘
otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x85e): undefined reference to `SQLDriverConnect‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_conn::logoff()‘:
otltest.cpp:(.text._ZN8otl_conn6logoffEv[_ZN8otl_conn6logoffEv]+0x57): undefined reference to `SQLDisconnect‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_conn::error(otl_exc&)‘:
otltest.cpp:(.text._ZN8otl_conn5errorER7otl_exc[_ZN8otl_conn5errorER7otl_exc]+0x67): undefined reference to `SQLGetDiagRec‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_conn::commit()‘:
otltest.cpp:(.text._ZN8otl_conn6commitEv[_ZN8otl_conn6commitEv]+0x22): undefined reference to `SQLEndTran‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::open(otl_conn&)‘:
otltest.cpp:(.text._ZN7otl_cur4openER8otl_conn[_ZN7otl_cur4openER8otl_conn]+0x4b): undefined reference to `SQLAllocHandle‘
otltest.cpp:(.text._ZN7otl_cur4openER8otl_conn[_ZN7otl_cur4openER8otl_conn]+0xa7): undefined reference to `SQLSetStmtAttr‘
otltest.cpp:(.text._ZN7otl_cur4openER8otl_conn[_ZN7otl_cur4openER8otl_conn]+0x100): undefined reference to `SQLSetStmtAttr‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::close(char)‘:
otltest.cpp:(.text._ZN7otl_cur5closeEc[_ZN7otl_cur5closeEc]+0x2d): undefined reference to `SQLFreeHandle‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::sql_row_count(long*)‘:
otltest.cpp:(.text._ZN7otl_cur13sql_row_countEPl[_ZN7otl_cur13sql_row_countEPl]+0x23): undefined reference to `SQLRowCount‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::parse(char*)‘:
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x498): undefined reference to `SQLTables‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x520): undefined reference to `SQLStatistics‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x566): undefined reference to `SQLGetTypeInfo‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x604): undefined reference to `SQLColumns‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x67c): undefined reference to `SQLProcedures‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x71a): undefined reference to `SQLColumnPrivileges‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x792): undefined reference to `SQLTablePrivileges‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x80a): undefined reference to `SQLPrimaryKeys‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x8a8): undefined reference to `SQLProcedureColumns‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x97e): undefined reference to `SQLForeignKeys‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0xa18): undefined reference to `SQLExecDirect‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0xc53): undefined reference to `SQLPrepare‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::exec(int, int, unsigned char)‘:
otltest.cpp:(.text._ZN7otl_cur4execEiih[_ZN7otl_cur4execEiih]+0x79): undefined reference to `SQLSetStmtAttr‘
otltest.cpp:(.text._ZN7otl_cur4execEiih[_ZN7otl_cur4execEiih]+0xe1): undefined reference to `SQLExecute‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::bind(char const*, otl_var&, int, int, int, int, int, int)‘:
otltest.cpp:(.text._ZN7otl_cur4bindEPKcR7otl_variiiiii[_ZN7otl_cur4bindEPKcR7otl_variiiiii]+0x16b): undefined reference to `SQLBindParameter‘
otltest.cpp:(.text._ZN7otl_cur4bindEPKcR7otl_variiiiii[_ZN7otl_cur4bindEPKcR7otl_variiiiii]+0x233): undefined reference to `SQLBindParameter‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::bind(int, otl_var&, int, int, int)‘:
otltest.cpp:(.text._ZN7otl_cur4bindEiR7otl_variii[_ZN7otl_cur4bindEiR7otl_variii]+0xe5): undefined reference to `SQLBindCol‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::describe_column(otl_column_desc&, int, int&)‘:
otltest.cpp:(.text._ZN7otl_cur15describe_columnER15otl_column_desciRi[_ZN7otl_cur15describe_columnER15otl_column_desciRi]+0x49): undefined reference to `SQLNumResultCols‘
otltest.cpp:(.text._ZN7otl_cur15describe_columnER15otl_column_desciRi[_ZN7otl_cur15describe_columnER15otl_column_desciRi]+0xf1): undefined reference to `SQLDescribeCol‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::error(otl_exc&)‘:
otltest.cpp:(.text._ZN7otl_cur5errorER7otl_exc[_ZN7otl_cur5errorER7otl_exc]+0x67): undefined reference to `SQLGetDiagRec‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_sel::close_select(otl_cur&)‘:
otltest.cpp:(.text._ZN7otl_sel12close_selectER7otl_cur[_ZN7otl_sel12close_selectER7otl_cur]+0x33): undefined reference to `SQLFreeStmt‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_sel::first(otl_cur&, int&, int&, int&, int&, int)‘:
otltest.cpp:(.text._ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i[_ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i]+0x52): undefined reference to `SQLSetStmtAttr‘
otltest.cpp:(.text._ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i[_ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i]+0xb5): undefined reference to `SQLSetStmtAttr‘
otltest.cpp:(.text._ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i[_ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i]+0x111): undefined reference to `SQLExecute‘
otltest.cpp:(.text._ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i[_ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i]+0x16c): undefined reference to `SQLFetchScroll‘
otltest.cpp:(.text._ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i[_ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i]+0x20e): undefined reference to `SQLFreeStmt‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_sel::next(otl_cur&, int&, int&, int&, int&, int)‘:
otltest.cpp:(.text._ZN7otl_sel4nextER7otl_curRiS2_S2_S2_i[_ZN7otl_sel4nextER7otl_curRiS2_S2_S2_i]+0x86): undefined reference to `SQLFreeStmt‘
otltest.cpp:(.text._ZN7otl_sel4nextER7otl_curRiS2_S2_S2_i[_ZN7otl_sel4nextER7otl_curRiS2_S2_S2_i]+0xca): undefined reference to `SQLFetchScroll‘
otltest.cpp:(.text._ZN7otl_sel4nextER7otl_curRiS2_S2_S2_i[_ZN7otl_sel4nextER7otl_curRiS2_S2_S2_i]+0x161): undefined reference to `SQLFreeStmt‘
collect2: error: ld returned 1 exit status
make[2]: *** [otltest2] Error 1
make[1]: *** [CMakeFiles/otltest2.dir/all] Error 2
make: *** [all] Error 2
[email protected]:/home/c2# 

  target_link_libraries(otltest2 libmyodbc5a.so)   程序生成.
[email protected]:/home/c2# make clean
[email protected]:/home/c2# cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/c2
[email protected]:/home/c2# make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/c2
[100%] Building CXX object CMakeFiles/otltest2.dir/otltest.cpp.o
Linking CXX executable otltest2
[100%] Built target otltest2
[email protected]:/home/c2# 

前面说过

  libmyodbc5w.so  是unicode 版本的.
  libmyodbc5a.so 是ansi版本的.

target_link_libraries(otltest2 libmyodbc5w.so) 时 出错,

otl_conn::rlogon(char const*, int) 说明 unicode 版本不能使用.

unicode 应该是 otl_conn::rlogon(wchar const*, int)

otl.h 中有个 OTL_UNICODE_EXCEPTION_AND_RLOGON 这个宏 ,有时间的朋友可以研究下说下方式.3Q.

[email protected]:/home/c2# make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/c2
[100%] Building CXX object CMakeFiles/otltest2.dir/otltest.cpp.o
Linking CXX executable otltest2
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_conn::rlogon(char const*, int)‘:
otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x64c): undefined reference to `SQLSetConnectAttr‘
otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x67d): undefined reference to `SQLSetConnectAttr‘
otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x6eb): undefined reference to `SQLSetConnectAttr‘
otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x765): undefined reference to `SQLConnect‘
otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x85e): undefined reference to `SQLDriverConnect‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_conn::error(otl_exc&)‘:
otltest.cpp:(.text._ZN8otl_conn5errorER7otl_exc[_ZN8otl_conn5errorER7otl_exc]+0x67): undefined reference to `SQLGetDiagRec‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::open(otl_conn&)‘:
otltest.cpp:(.text._ZN7otl_cur4openER8otl_conn[_ZN7otl_cur4openER8otl_conn]+0xa7): undefined reference to `SQLSetStmtAttr‘
otltest.cpp:(.text._ZN7otl_cur4openER8otl_conn[_ZN7otl_cur4openER8otl_conn]+0x100): undefined reference to `SQLSetStmtAttr‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::parse(char*)‘:
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x498): undefined reference to `SQLTables‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x520): undefined reference to `SQLStatistics‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x566): undefined reference to `SQLGetTypeInfo‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x604): undefined reference to `SQLColumns‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x67c): undefined reference to `SQLProcedures‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x71a): undefined reference to `SQLColumnPrivileges‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x792): undefined reference to `SQLTablePrivileges‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x80a): undefined reference to `SQLPrimaryKeys‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x8a8): undefined reference to `SQLProcedureColumns‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x97e): undefined reference to `SQLForeignKeys‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0xa18): undefined reference to `SQLExecDirect‘
otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0xc53): undefined reference to `SQLPrepare‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::exec(int, int, unsigned char)‘:
otltest.cpp:(.text._ZN7otl_cur4execEiih[_ZN7otl_cur4execEiih]+0x79): undefined reference to `SQLSetStmtAttr‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::describe_column(otl_column_desc&, int, int&)‘:
otltest.cpp:(.text._ZN7otl_cur15describe_columnER15otl_column_desciRi[_ZN7otl_cur15describe_columnER15otl_column_desciRi]+0xf1): undefined reference to `SQLDescribeCol‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::error(otl_exc&)‘:
otltest.cpp:(.text._ZN7otl_cur5errorER7otl_exc[_ZN7otl_cur5errorER7otl_exc]+0x67): undefined reference to `SQLGetDiagRec‘
CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_sel::first(otl_cur&, int&, int&, int&, int&, int)‘:
otltest.cpp:(.text._ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i[_ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i]+0x52): undefined reference to `SQLSetStmtAttr‘
otltest.cpp:(.text._ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i[_ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i]+0xb5): undefined reference to `SQLSetStmtAttr‘
collect2: error: ld returned 1 exit status
make[2]: *** [otltest2] Error 1
make[1]: *** [CMakeFiles/otltest2.dir/all] Error 2
make: *** [all] Error 2
[email protected]:/home/c2# 

但 unixodbc 的ini 配置 文件

/usr/local/etc/odbcinst.ini  配置:

[mysqlodbc]      #Section
Driver=/usr/local/lib/libmyodbc5w.so      #libmyodbc5w.so 和libmyodbc5a.so的区别
SETUP=/usr/local/lib/libmyodbc5w.so
UsageCount=1
  这儿配置 libmyodbc5w.so 和libmyodbc5a.so 都能正常访问.

  运行 otltest2

[email protected]:/home/c2# ./otltest2
hello
id=1
user=杩姣
name=灏3
id=2
user=rq
name=fasdf
id=3
user=灏寮?
name=xiaoZ

[email protected]:/home/c2#


linux 下不能显示中文.

另一个程序可以在网页中显示:


至此 在debian x64环境下通过C++ 的OTL库去访问mysql已经完成.

时间: 2024-11-26 08:22:52

debian C++ OTL库 用 unixodbc 连接 mysql 小记的相关文章

centos7 unixodbc 连接mysql

1.yum install unixODBC unixODBC-devel libtool-ltdl libtool-ltdl-devel 2.yum install mysql-connector-odbc 3./etc/odbcinst.ini [freeswitch] Driver=MySQLSERVER=192.168.16.4PORT=3306DATABASE=freeswitchUSER=rootPASSWORD=123456Socket=/var/lib/mysql/mysql.s

Python连接Mysql数据库(Debian)

Python连接Mysql数据库(Debian) 以下是Python 2.*版本的安装方法,MySQL-python暂不支持Python 3.*版本 提前要做的工作: 安装setuptools,在终端中运行 wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python 安装pip,下载“get_pip.py”,运行 python get_pip.py 运行如下命令装好必要的包 sudo apt-get install python-d

MySQLdb库连接MySQL数据库

Cacti是一套基于PHP,MySQL,SNMP及RRDTool开发的网络流量监测图形分析工具.Cacti是通过 snmpget来获取数据,使用 RRDtool绘画图形,而且你完全可以不需要了解RRDtool复杂的参数.它提供了非常强大的数据和用户管理功能,可以指定每一个用户能查看树状结构.host以及任何一张图,还可以与LDAP结合进行用户验证,同时也能自己增加模板,功能非常强大完善.界面友好.软件 Cacti 的发展是基于让 RRDTool 使用者更方便使用该软件,除了基本的 Snmp 流量

[Oracle, MySQL] Oracle通过dblink连接MySQL

http://blog.csdn.net/dbanote/article/details/10488581 版权声明:本文为博主原创文章,未经博主允许不得转载. 业务上有这么一个需求,需要把Oracle的一些数据同步到MySQL,如果每次都是手动同步的话,实在太麻烦,因此花了点时间研究了下Oracle直连MySQL的方式. 参考文档:Detailed Overview of Connecting Oracle to MySQL Using DG4ODBC Database Link (Doc I

otl库(以前不知道有这个库,并且还可以在Unix下使用)

OTL介绍:OTL 是 Oracle, Odbc and DB2-CLI Template Library 的缩写,是一个C++编译中操控关系数据库的模板库,它目前几乎支持所有的当前各种主流数据库,例如Oracle, MS SQL Server, Sybase, Informix, MySQL, DB2, Interbase / Firebird, PostgreSQL, SQLite, SAP/DB, TimesTen, MS ACCESS等等.OTL中直接操作Oracle主要是通过Oracl

Ruby 连接MySQL数据库

使用Ruby连接数据库的过程还真的是坎坷,于是写点文字记录一下. 简介 Ruby简介 RubyGems简介 包管理之道 比较著名的包管理举例 细说gem 常用的命令 准备 驱动下载 dbi mysql mysql2 MySQL的CC连接器 测试环境 代码测试 连接数据库 CRUD Select Delete Update Insert 仿PreparedStatement 假冒伪劣版 假冒伪劣进阶版 处理结果集 表结构 结果集遍历 总结 简介 Ruby简介 Ruby是一种纯粹的面向对象编程语言.

mybatis连接mysql数据库插入中文乱码

对于mysql数据库的乱码问题,有两中情况: 1. mysql数据库编码问题(建库时设定). 2. 连接mysql数据库的url编码设置问题. 对于第一个问题,目前个人发现只能通过重新建库解决,建库的时候,选择UTF-8字符集.我试过修改现有数据库字符集为UFT -8,但是根本不起作用,插入的中文仍然乱码(中文显示成:???).重建库时选择字符集为UTF-8之后,中文正常显示了. 对于第二个问题,是这样 的情况:我建库时设置了数据库默认字符集为UTF-8,通过mysql workbench直接插

远程连接mysql 授权方法详解

今在服务器上有mysql数据库,远程访问,不想公布root账户,所以,创建了demo账户,允许demo账户在任何地方都能访问mysql数据库中shandong库,接下来为您详细介绍 今在服务器上 有mysql 数据库,远程访问,不想公布root账户,所以,创建了demo账户,允许demo账户在任何地方都能访问mysql数据库中shandong库. 方案一: 在安装mysql的机器上运行: 1: 创建user用户 复制代码 代码如下: CREATE USER demo IDENTIFIED BY

通过java连接mysql总结

简介:通过java连接mysql需要用到JDBC驱动.JDBC驱动安装在mysql-client客户端,mysql-server服务端不需要安装. 数据库安装初始化略.测试用到的命令: 1.创建测试用库,表,字段: create database score; use score; create table score(id nvarchar(10),stu_id nvarchar(10),c_name nvarchar(10),grade nvarchar(10)); insert into