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

参考网址:

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

root@debian:~# uname -a
Linux debian 3.2.0-4-amd64 #1 SMP Debian 3.2.65-1 x86_64 GNU/Linux

mysql

root@debian:~# mysql --version
mysql Ver 14.14 Distrib 5.5.43, for debian-linux-gnu (x86_64) using readline 6.2

unixODBC

root@debian:~# 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

  1. ./configure
  2. make
  3. 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

  1.  

安装对应的数据库驱动。比如mysql 对应 libmyodbc。 安装完后查看下库包含的文件就可以找到对应的lib
--此次强烈建议用源码安装。ubuntu 安装很简单apt-get就好,rpm 包会有依赖问题,YUM 有些包会冲突。
tar zxvf mysql-connector-odbc-5.1.11-linux-el6-x86-64bit.tar.gz
cd mysql-connector-odbc-5.1.11-linux-el6-x86-64bit\lib

#复制到 /usr/local/lib

cp ./*.*  /usr/local/lib

  1.  

2.1 mysql 测试前的准备.

  

  1. #使用mysql数据库
  2. use mysql;
  3.  
  4. #查询
  5. select host,user,password from user;
  6.  
  7. #更新root密码
  8. update user set password=PASSWORD('') where user='root' ;
  9.  
  10. #刷新
  11. flush privileges;
  12.  
  13. #创建新用户,设置任何地方都可以访问.
  14. #命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password';
  15. create user 'dbadmin'@'%' identified by 'aaaaaa';
  16.  
  17. #创建数据库 test1,设置utf8格式编码
  18. Create DATABASE IF NOT EXISTS test1 default charset utf8 COLLATE utf8_general_ci;
  19.  
  20. #显示数据库
  21. show databases;
  22.  
  23. #设置数据库test1的所有权限给 dbadmin用户 .
  24. grant all on test1.* to 'dbadmin'@'%' ;
  25.  
  26. #刷新
  27. flush privileges;

mysql配置文件my.cnf详解 :

http://blog.chinaunix.net/uid-20639775-id-154429.html

http://blog.sina.com.cn/s/blog_a8b2435901016f5f.html

mysql 更改数据库目录:

http://blog.chinaunix.net/uid-77311-id-3485225.html

mysql 更改数据编码:

http://www.pc6.com/infoview/Article_63586.html

http://blog.chinaunix.net/uid-223060-id-2127099.html

最后/etc/mysql/my.cnf 的内容为:

  1. #
  2. # The MySQL database server configuration file.
  3. #
  4. # You can copy this to one of:
  5. # - "/etc/mysql/my.cnf" to set global options,
  6. # - "~/.my.cnf" to set user-specific options.
  7. #
  8. # One can use all long options that the program supports.
  9. # Run program with --help to get a list of available options and with
  10. # --print-defaults to see which it would actually understand and use.
  11. #
  12. # For explanations see
  13. # http://dev.mysql.com/doc/mysql/en/server-system-variables.html
  14.  
  15. # This will be passed to all mysql clients
  16. # It has been reported that passwords should be enclosed with ticks/quotes
  17. # escpecially if they contain "#" chars...
  18. # Remember to edit /etc/mysql/debian.cnf when changing the socket location.
  19. [client]
  20. port = 3306
  21. socket = /var/run/mysqld/mysqld.sock
  22.  
  23. #设置编译
  24. default-character-set = utf8
  25.  
  26. # Here is entries for some specific programs
  27. # The following values assume you have at least 32M ram
  28.  
  29. # This was formally known as [safe_mysqld]. Both versions are currently parsed.
  30. [mysqld_safe]
  31. socket = /var/run/mysqld/mysqld.sock
  32. nice = 0
  33.  
  34. [mysqld]
  35. #
  36. # * Basic Settings
  37. #
  38. user = mysql
  39. pid-file = /var/run/mysqld/mysqld.pid
  40. socket = /var/run/mysqld/mysqld.sock
  41. port = 3306
  42. basedir = /usr
  43. #datadir = /var/lib/mysql #原数据库目录
  44. datadir = /xxx/x/xxx/mysql #现数据库目录
  45. tmpdir = /tmp
  46. lc-messages-dir = /usr/share/mysql
  47.  
  48. #设置编译
  49. default-storage-engine=INNODB
  50. character-set-server=utf8
  51. collation-server=utf8_general_ci
  52.  
  53. skip-external-locking
  54. #
  55. # Instead of skip-networking the default is now to listen only on
  56. # localhost which is more compatible and is not less secure.
  57. #bind-address = 127.0.0.1
  58. #
  59. # * Fine Tuning
  60. #
  61. key_buffer = 16M
  62. max_allowed_packet = 16M
  63. thread_stack = 192K
  64. thread_cache_size = 8
  65. # This replaces the startup script and checks MyISAM tables if needed
  66. # the first time they are touched
  67. myisam-recover = BACKUP
  68. #max_connections = 100
  69. #table_cache = 64
  70. #thread_concurrency = 10
  71. #
  72. # * Query Cache Configuration
  73. #
  74. query_cache_limit = 1M
  75. query_cache_size = 16M
  76. #
  77. # * Logging and Replication
  78. #
  79. # Both location gets rotated by the cronjob.
  80. # Be aware that this log type is a performance killer.
  81. # As of 5.1 you can enable the log at runtime!
  82. #general_log_file = /var/log/mysql/mysql.log
  83. #general_log = 1
  84. #
  85. # Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf.
  86. #
  87. # Here you can see queries with especially long duration
  88. #log_slow_queries = /var/log/mysql/mysql-slow.log
  89. #long_query_time = 2
  90. #log-queries-not-using-indexes
  91. #
  92. # The following can be used as easy to replay backup logs or for replication.
  93. # note: if you are setting up a replication slave, see README.Debian about
  94. # other settings you may need to change.
  95. #server-id = 1
  96. #log_bin = /var/log/mysql/mysql-bin.log
  97. expire_logs_days = 10
  98. max_binlog_size = 100M
  99. #binlog_do_db = include_database_name
  100. #binlog_ignore_db = include_database_name
  101. #
  102. # * InnoDB
  103. #
  104. # InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
  105. # Read the manual for more InnoDB related options. There are many!
  106. #
  107. # * Security Features
  108. #
  109. # Read the manual, too, if you want chroot!
  110. # chroot = /var/lib/mysql/
  111. #
  112. # For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
  113. #
  114. # ssl-ca=/etc/mysql/cacert.pem
  115. # ssl-cert=/etc/mysql/server-cert.pem
  116. # ssl-key=/etc/mysql/server-key.pem
  117.  
  118. [mysqldump]
  119. quick
  120. quote-names
  121. max_allowed_packet = 16M
  122.  
  123. [mysql]
  124. #no-auto-rehash # faster start of mysql but no tab completition
  125.  
  126. [isamchk]
  127. key_buffer = 16M
  128.  
  129. #
  130. # * IMPORTANT: Additional settings that can override those from this file!
  131. # The files must end with '.cnf', otherwise they'll be ignored.
  132. #
  133. !includedir /etc/mysql/conf.d/

  

配置unixodbc

UNIXODBC 安装好后会有:

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

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

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

  1.  

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

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

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

找到了相应的答案.

"

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

"

  1.   libmyodbc5w.so unicode 版本的.
  1.   libmyodbc5a.so ansi版本的.
  2.  

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

#Section
[TEST]    
#Driver=/usr/local/lib/libmyodbc5a.so
Description = The Database for mysql
Trace = On
TraceFile = stderr
#odbcinst.ini 中指定的Section
Driver = mysqlodbc
#数据库IP
SERVER = localhost
#用户名
USER = sa2
#密码    
PASSWORD = 111111
#数据库端口
PORT = 3306
#要连接的数据库     
DATABASE = test1
#虽然这儿指定 mysqld.sock位置,但后面还是会报错,下文有相应解决办法.
socket=/var/run/mysqld/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

  查看:

  

  1. root@debian:/home/c2# ls -l /tmp
  2. total 0
  3. lrwxrwxrwx 1 root root 27 Jul 10 00:39 mysql.sock -> /var/run/mysqld/mysqld.sock

  连接成功.

  再运行 命令:

  1. root@debian:~# isql TEST -v
  2. +---------------------------------------+
  3. | Connected! |
  4. | |
  5. | sql-statement |
  6. | help [tablename] |
  7. | quit |
  8. | |
  9. +---------------------------------------+
  10. SQL> select * from t1;
  11. +-----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  12. | Id | name | name2 |
  13. +-----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  14. | 1 | 杩姣 | 3 |
  15. | 2 | rq | fasdf |
  16. | 3 | 灏寮? | xiaoZ |
  17. +-----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  18. SQLRowCount returns 3
  19. 3 rows fetched
  20. SQL>

t1表的视图:

通过OTL连接mysql

目录内容如下:

otltest.cpp内容如下:

  1. #include <iostream>
  2. using namespace std;
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. #include <stdio.h>
  8.  
  9. // Thefollowing #define is required with MyODBC 5.1 and higher
  10. //#define OTL_ODBC_SELECT_STM_EXECUTE_BEFORE_DESCRIBE
  11.  
  12. #define OTL_ODBC // CompileOTL 4.0/ODBC
  13. #define OTL_ODBC_UNIX // uncomment this line if UnixODBC is used
  14. //#define OTL_UNICODE // Compile OTL with Unicode --->屏蔽 使用时中文显示乱码
  15.  
  16. #include "otlv4.h" // include the OTL 4.0 header file
  17.  
  18. otl_connect db; // connect object
  19.  
  20. void select()
  21. {
  22. try{
  23. otl_stream ostream1(500, // buffer size
  24. "select * from t1 ",
  25. // SELECT statement
  26. db // connect object
  27. );
  28. // create select stream
  29.  
  30. int id;
  31. unsigned char user[255];
  32. unsigned char name[255];
  33.  
  34. while(!ostream1.eof())
  35. { // while not end-of-data
  36. //ostream1>>id>>user>>name;
  37. ostream1>>id;
  38. ostream1>>user;
  39. ostream1>>name;
  40. cout<<"id="<<id<<endl;
  41. cout<<"user="<<user<<endl;
  42. cout<<"name="<<name<<endl;
  43.  
  44. }
  45. }
  46.  
  47. catch(otl_exception& p)
  48. { // intercept OTL exceptions
  49.  
  50. cout<<"otl_exception:"<<endl;
  51. cerr<<p.msg<<endl; // print out error message
  52. cerr<<p.stm_text<<endl; // print out SQL that caused the error
  53. cerr<<p.var_info<<endl; // print out the variable that caused the error
  54. }
  55.  
  56. }
  57.  
  58. int main()
  59. {
  60.  
  61. cout<<"hello"<<endl;
  62. otl_connect::otl_initialize(); // initialize the database API environment
  63. try{
  64.  
  65. db.rlogon("sa2/111111@TEST"); // connect to the database 这儿是 用户名/密码@section名 (/usr/local/etc/odbc.ini  的section名)
  66.  
  67. select(); // select records from table
  68.  
  69. }
  70.  
  71. catch(otl_exception& p){ // intercept OTL exceptions
  72. cerr<<p.msg<<endl; // print out error message
  73. cerr<<p.stm_text<<endl; // print out SQL that caused the error
  74. cerr<<p.var_info<<endl; // print out the variable that caused the error
  75. }
  76.  
  77. db.logoff(); // disconnect from the database
  78.  
  79. return 0;
  80.  
  81. }

CMakeLists.txt 内容:

  1. cmake_minimum_required (VERSION 2.6)
  2. project (otltest2)
  3. add_executable(otltest2 otltest.cpp)
  4. target_link_libraries(otltest2 libmyodbc5a.so) --->
  1.  
  1.   一定要添加 libmyodbc5a.so 否则会出现 SQLFreeHandle 之类的错误
  2.  
  1. root@debian:/home/c2# make
  2. -- Configuring done
  3. -- Generating done
  4. -- Build files have been written to: /home/c2
  5. Scanning dependencies of target otltest2
  6. [100%] Building CXX object CMakeFiles/otltest2.dir/otltest.cpp.o
  7. Linking CXX executable otltest2
  8. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_conn::~otl_conn()':
  9. otltest.cpp:(.text._ZN8otl_connD2Ev[_ZN8otl_connD5Ev]+0x63): undefined reference to `SQLFreeHandle'
  10. otltest.cpp:(.text._ZN8otl_connD2Ev[_ZN8otl_connD5Ev]+0x9b): undefined reference to `SQLFreeHandle'
  11. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_conn::rlogon(char const*, int)':
  12. otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x532): undefined reference to `SQLAllocHandle'
  13. otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x588): undefined reference to `SQLSetEnvAttr'
  14. otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x5df): undefined reference to `SQLAllocHandle'
  15. otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x64c): undefined reference to `SQLSetConnectAttr'
  16. otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x67d): undefined reference to `SQLSetConnectAttr'
  17. otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x6eb): undefined reference to `SQLSetConnectAttr'
  18. otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x765): undefined reference to `SQLConnect'
  19. otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x85e): undefined reference to `SQLDriverConnect'
  20. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_conn::logoff()':
  21. otltest.cpp:(.text._ZN8otl_conn6logoffEv[_ZN8otl_conn6logoffEv]+0x57): undefined reference to `SQLDisconnect'
  22. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_conn::error(otl_exc&)':
  23. otltest.cpp:(.text._ZN8otl_conn5errorER7otl_exc[_ZN8otl_conn5errorER7otl_exc]+0x67): undefined reference to `SQLGetDiagRec'
  24. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_conn::commit()':
  25. otltest.cpp:(.text._ZN8otl_conn6commitEv[_ZN8otl_conn6commitEv]+0x22): undefined reference to `SQLEndTran'
  26. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::open(otl_conn&)':
  27. otltest.cpp:(.text._ZN7otl_cur4openER8otl_conn[_ZN7otl_cur4openER8otl_conn]+0x4b): undefined reference to `SQLAllocHandle'
  28. otltest.cpp:(.text._ZN7otl_cur4openER8otl_conn[_ZN7otl_cur4openER8otl_conn]+0xa7): undefined reference to `SQLSetStmtAttr'
  29. otltest.cpp:(.text._ZN7otl_cur4openER8otl_conn[_ZN7otl_cur4openER8otl_conn]+0x100): undefined reference to `SQLSetStmtAttr'
  30. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::close(char)':
  31. otltest.cpp:(.text._ZN7otl_cur5closeEc[_ZN7otl_cur5closeEc]+0x2d): undefined reference to `SQLFreeHandle'
  32. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::sql_row_count(long*)':
  33. otltest.cpp:(.text._ZN7otl_cur13sql_row_countEPl[_ZN7otl_cur13sql_row_countEPl]+0x23): undefined reference to `SQLRowCount'
  34. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::parse(char*)':
  35. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x498): undefined reference to `SQLTables'
  36. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x520): undefined reference to `SQLStatistics'
  37. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x566): undefined reference to `SQLGetTypeInfo'
  38. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x604): undefined reference to `SQLColumns'
  39. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x67c): undefined reference to `SQLProcedures'
  40. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x71a): undefined reference to `SQLColumnPrivileges'
  41. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x792): undefined reference to `SQLTablePrivileges'
  42. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x80a): undefined reference to `SQLPrimaryKeys'
  43. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x8a8): undefined reference to `SQLProcedureColumns'
  44. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x97e): undefined reference to `SQLForeignKeys'
  45. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0xa18): undefined reference to `SQLExecDirect'
  46. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0xc53): undefined reference to `SQLPrepare'
  47. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::exec(int, int, unsigned char)':
  48. otltest.cpp:(.text._ZN7otl_cur4execEiih[_ZN7otl_cur4execEiih]+0x79): undefined reference to `SQLSetStmtAttr'
  49. otltest.cpp:(.text._ZN7otl_cur4execEiih[_ZN7otl_cur4execEiih]+0xe1): undefined reference to `SQLExecute'
  50. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::bind(char const*, otl_var&, int, int, int, int, int, int)':
  51. otltest.cpp:(.text._ZN7otl_cur4bindEPKcR7otl_variiiiii[_ZN7otl_cur4bindEPKcR7otl_variiiiii]+0x16b): undefined reference to `SQLBindParameter'
  52. otltest.cpp:(.text._ZN7otl_cur4bindEPKcR7otl_variiiiii[_ZN7otl_cur4bindEPKcR7otl_variiiiii]+0x233): undefined reference to `SQLBindParameter'
  53. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::bind(int, otl_var&, int, int, int)':
  54. otltest.cpp:(.text._ZN7otl_cur4bindEiR7otl_variii[_ZN7otl_cur4bindEiR7otl_variii]+0xe5): undefined reference to `SQLBindCol'
  55. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::describe_column(otl_column_desc&, int, int&)':
  56. otltest.cpp:(.text._ZN7otl_cur15describe_columnER15otl_column_desciRi[_ZN7otl_cur15describe_columnER15otl_column_desciRi]+0x49): undefined reference to `SQLNumResultCols'
  57. otltest.cpp:(.text._ZN7otl_cur15describe_columnER15otl_column_desciRi[_ZN7otl_cur15describe_columnER15otl_column_desciRi]+0xf1): undefined reference to `SQLDescribeCol'
  58. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::error(otl_exc&)':
  59. otltest.cpp:(.text._ZN7otl_cur5errorER7otl_exc[_ZN7otl_cur5errorER7otl_exc]+0x67): undefined reference to `SQLGetDiagRec'
  60. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_sel::close_select(otl_cur&)':
  61. otltest.cpp:(.text._ZN7otl_sel12close_selectER7otl_cur[_ZN7otl_sel12close_selectER7otl_cur]+0x33): undefined reference to `SQLFreeStmt'
  62. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_sel::first(otl_cur&, int&, int&, int&, int&, int)':
  63. otltest.cpp:(.text._ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i[_ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i]+0x52): undefined reference to `SQLSetStmtAttr'
  64. otltest.cpp:(.text._ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i[_ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i]+0xb5): undefined reference to `SQLSetStmtAttr'
  65. otltest.cpp:(.text._ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i[_ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i]+0x111): undefined reference to `SQLExecute'
  66. otltest.cpp:(.text._ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i[_ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i]+0x16c): undefined reference to `SQLFetchScroll'
  67. otltest.cpp:(.text._ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i[_ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i]+0x20e): undefined reference to `SQLFreeStmt'
  68. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_sel::next(otl_cur&, int&, int&, int&, int&, int)':
  69. otltest.cpp:(.text._ZN7otl_sel4nextER7otl_curRiS2_S2_S2_i[_ZN7otl_sel4nextER7otl_curRiS2_S2_S2_i]+0x86): undefined reference to `SQLFreeStmt'
  70. otltest.cpp:(.text._ZN7otl_sel4nextER7otl_curRiS2_S2_S2_i[_ZN7otl_sel4nextER7otl_curRiS2_S2_S2_i]+0xca): undefined reference to `SQLFetchScroll'
  71. otltest.cpp:(.text._ZN7otl_sel4nextER7otl_curRiS2_S2_S2_i[_ZN7otl_sel4nextER7otl_curRiS2_S2_S2_i]+0x161): undefined reference to `SQLFreeStmt'
  72. collect2: error: ld returned 1 exit status
  73. make[2]: *** [otltest2] Error 1
  74. make[1]: *** [CMakeFiles/otltest2.dir/all] Error 2
  75. make: *** [all] Error 2
  76. root@debian:/home/c2#

  1.   target_link_libraries(otltest2 libmyodbc5a.so)
      程序生成.
  1. root@debian:/home/c2# make clean
  2. root@debian:/home/c2# cmake .
  3. -- Configuring done
  4. -- Generating done
  5. -- Build files have been written to: /home/c2
  6. root@debian:/home/c2# make
  7. -- Configuring done
  8. -- Generating done
  9. -- Build files have been written to: /home/c2
  10. [100%] Building CXX object CMakeFiles/otltest2.dir/otltest.cpp.o
  11. Linking CXX executable otltest2
  12. [100%] Built target otltest2
  13. root@debian:/home/c2#

前面说过

  1.   libmyodbc5w.so unicode 版本的.
  1.   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.

  1. root@debian:/home/c2# make
  2. -- Configuring done
  3. -- Generating done
  4. -- Build files have been written to: /home/c2
  5. [100%] Building CXX object CMakeFiles/otltest2.dir/otltest.cpp.o
  6. Linking CXX executable otltest2
  7. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_conn::rlogon(char const*, int)':
  8. otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x64c): undefined reference to `SQLSetConnectAttr'
  9. otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x67d): undefined reference to `SQLSetConnectAttr'
  10. otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x6eb): undefined reference to `SQLSetConnectAttr'
  11. otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x765): undefined reference to `SQLConnect'
  12. otltest.cpp:(.text._ZN8otl_conn6rlogonEPKci[_ZN8otl_conn6rlogonEPKci]+0x85e): undefined reference to `SQLDriverConnect'
  13. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_conn::error(otl_exc&)':
  14. otltest.cpp:(.text._ZN8otl_conn5errorER7otl_exc[_ZN8otl_conn5errorER7otl_exc]+0x67): undefined reference to `SQLGetDiagRec'
  15. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::open(otl_conn&)':
  16. otltest.cpp:(.text._ZN7otl_cur4openER8otl_conn[_ZN7otl_cur4openER8otl_conn]+0xa7): undefined reference to `SQLSetStmtAttr'
  17. otltest.cpp:(.text._ZN7otl_cur4openER8otl_conn[_ZN7otl_cur4openER8otl_conn]+0x100): undefined reference to `SQLSetStmtAttr'
  18. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::parse(char*)':
  19. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x498): undefined reference to `SQLTables'
  20. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x520): undefined reference to `SQLStatistics'
  21. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x566): undefined reference to `SQLGetTypeInfo'
  22. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x604): undefined reference to `SQLColumns'
  23. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x67c): undefined reference to `SQLProcedures'
  24. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x71a): undefined reference to `SQLColumnPrivileges'
  25. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x792): undefined reference to `SQLTablePrivileges'
  26. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x80a): undefined reference to `SQLPrimaryKeys'
  27. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x8a8): undefined reference to `SQLProcedureColumns'
  28. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0x97e): undefined reference to `SQLForeignKeys'
  29. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0xa18): undefined reference to `SQLExecDirect'
  30. otltest.cpp:(.text._ZN7otl_cur5parseEPc[_ZN7otl_cur5parseEPc]+0xc53): undefined reference to `SQLPrepare'
  31. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::exec(int, int, unsigned char)':
  32. otltest.cpp:(.text._ZN7otl_cur4execEiih[_ZN7otl_cur4execEiih]+0x79): undefined reference to `SQLSetStmtAttr'
  33. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::describe_column(otl_column_desc&, int, int&)':
  34. otltest.cpp:(.text._ZN7otl_cur15describe_columnER15otl_column_desciRi[_ZN7otl_cur15describe_columnER15otl_column_desciRi]+0xf1): undefined reference to `SQLDescribeCol'
  35. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_cur::error(otl_exc&)':
  36. otltest.cpp:(.text._ZN7otl_cur5errorER7otl_exc[_ZN7otl_cur5errorER7otl_exc]+0x67): undefined reference to `SQLGetDiagRec'
  37. CMakeFiles/otltest2.dir/otltest.cpp.o: In function `otl_sel::first(otl_cur&, int&, int&, int&, int&, int)':
  38. otltest.cpp:(.text._ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i[_ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i]+0x52): undefined reference to `SQLSetStmtAttr'
  39. otltest.cpp:(.text._ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i[_ZN7otl_sel5firstER7otl_curRiS2_S2_S2_i]+0xb5): undefined reference to `SQLSetStmtAttr'
  40. collect2: error: ld returned 1 exit status
  41. make[2]: *** [otltest2] Error 1
  42. make[1]: *** [CMakeFiles/otltest2.dir/all] Error 2
  43. make: *** [all] Error 2
  44. root@debian:/home/c2#

但 unixodbc 的ini 配置 文件

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

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

root@debian:/home/c2# ./otltest2
hello
id=1
user=杩姣
name=灏3
id=2
user=rq
name=fasdf
id=3
user=灏寮?
name=xiaoZ

root@debian:/home/c2#

  1.  

linux 下不能显示中文.

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

  1.  

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

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

  1. JAVA-数据库之JDBC连接MySQL数据库

    相关资料:<21天学通Java Web开发> JDBC连接MySQL数据库1.如果需要通过JDBC来连接MySQL数据库,还必须先在MySQL数据库服务器中创建数据库和表. Connect ...

  2. Python 使用PyMySql 库 连接MySql数据库时 查询中文遇到的乱码问题(实测可行) python 连接 MySql 中文乱码 pymysql库

    最近所写的代码中需要用到python去连接MySql数据库,因为是用PyQt5来构建的GUI,原本打算使用PyQt5中的数据库连接方法,后来虽然能够正确连接上发现还是不能提交修改内容,最后在qq交流群 ...

  3. 使用第三方库连接MySql数据库:PyMysql库和Pandas库

    使用PyMysql库和Pandas库链接Mysql 1 系统环境 系统版本:Win10 64位 Mysql版本: 8.0.15 MySQL Community Server - GPL pymysql ...

  4. Robotframework使用自写库连接mysql数据库

    Robotframework使用自写库连接mysql数据库 新建库文件mysqltest.py 代码如下: # -*- coding: utf-8 -*- import MySQLdbimport o ...

  5. 没有ORM库的时候,通过PDO连接MySQL的方法

    $pdo = new PDO("mysql:host=localhost;dbname=eq","root","root"); $pdo-& ...

  6. 【Python + Mysql】之用pymysql库连接Mysql数据库并进行增删改查操作

    用pip下载pymysql并引用 具体请参考文章: <Python之MySQL数据库增删改查操作> <python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删 ...

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

    OTL介绍:OTL 是 Oracle, Odbc and DB2-CLI Template Library 的缩写,是一个C++编译中操控关系数据库的模板库,它目前几乎支持所有的当前各种主流数据库,例 ...

  8. Ruby 连接MySQL数据库

    使用Ruby连接数据库的过程还真的是坎坷,于是写点文字记录一下. 简介 Ruby简介 RubyGems简介 包管理之道 比较著名的包管理举例 细说gem 常用的命令 准备 驱动下载 dbi mysql ...

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

    http://blog.csdn.net/dbanote/article/details/10488581 版权声明:本文为博主原创文章,未经博主允许不得转载. 业务上有这么一个需求,需要把Oracl ...

随机推荐

  1. 深入理解javascript原型和闭包(8)——简述【执行上下文】上

    什么是“执行上下文”(也叫做“执行上下文环境”)?暂且不下定义,先看一段代码: 第一句报错,a未定义,很正常.第二句.第三句输出都是undefined,说明浏览器在执行console.log(a)时, ...

  2. entiryFramework 事务控制

    1.在项目中添加System.Transactions命名空间 2.在代码中编写如下代码段: using (var trans = new TransactionScope()) { EF的代码 tr ...

  3. 关于学习JavaScript 的 高三编程 一些心得

    面对JS 问题来说,很多的细节问题以及 弱类型转换的问题,往往会成为学习js 路上的一个阻碍. 那么问题来了,今天我看到的是  高三 里面的  基本概念的 语法问题. 直奔主题.(还是帖代码先) sw ...

  4. 关于war包 jar包 ear包 及打包方法

    关于war包 jar包 ear包 及打包方法 war包:是做好一个web应用后,通常是网站打成包部署到容器中 jar包:通常是开发的时候要引用的通用类,打成包便于存放管理. ear包:企业级应用 通常 ...

  5. sqilite学习

    1,用代码插入数据 for (int i = 0; i < 100; i++) {        NSString *nameStr = [NSString stringWithFormat:@ ...

  6. 练习:使用nmcli 配置网络连接

    显示所有连接 # nmcli con show 显示活动连接的所有配置信息 # nmcli con show "System eth0" --->引号内为连接的网卡名称 显示 ...

  7. toolkit --------chart属性

    Data Visualization类组件以直观的图表方式显示数据的分布,能够让我们更好地分析各数据的内在联系.本文主要向大家介绍该类组件的基本特性以及使用实例. 一.基本特性介绍 1.chart组件 ...

  8. PhpStorm 集成 开源中国(oschina.net)的Git项目,提交SVN时注意事项

    第一步:配置 git.exe File -> Default Settings -> Version Control -> Git -> Path go Git executa ...

  9. QuickSort 快速排序 基于伪代码实现

    本文原创,转载请注明地址 http://www.cnblogs.com/baokang/p/4737492.html 伪代码 quicksort(A, lo, hi) if lo < hi p ...

  10. ubuntu和centos安装RRDTool——cacti前置技能

    Installing Pre-Requisites Note that RRDTool 1.0.x versions included all dependancies, but 1.2.x vers ...