这个东东也是折腾了几天,网上很多文章可能已经过时,所以写下不同,以备后用.
参考网址:
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已经完成.