OTL,大家都知道,高效的操作数据库的模板库,在windows下使用非常方便,但在linux下使用需要配置ODBC,找了好久, 网络上这方面的资料不是很全面,自己配置的时候也发现了很多问题,现在把配置中整理的资料分享出来,供大家参考,下面一些配置信息都差不多,就不再重复编写了,借鉴了网络上的一些步骤,重点是解决配置过程中需要注意的地方和编译测试代码出现的问题:
[MySQL]
Description = ODBC for MySQL
driver = /usr/lib/i386-linux-gnu/odbc/libmyodbc.so //注意,这里的库路径文件一定要写对,这是我本地安装好后的路径
Setup = /usr/lib/i386-linux-gnu/odbc/libmyodbc.so
FileUsage = 1
配置信息依次是驱动程序描述、驱动程序位置、配置程序位置、驱动程序使用次数。实际的驱动程序位置依Linux发行版的不同而有所差异。 Ubuntu通常位于/usr/lib/odbc/下。Suse位于/usr/lib/unixODBC/。RedHat等发行版可能有所不同。
然后是配置DSN,接下来我们创建一个名为HustMysqlDB的DSN。编辑usr/local/etc/odbc.ini文件
[PosData]
Description = The Database for PosData
Trace = On
TraceFile = stderr
Driver = MySQL
SERVER = localhost
USER = pos
PASSWORD = 123456
PORT = 3306
DATABASE = pos_data //要链接的数据库在配置文件里,DSN的名字即为Section的名字。在配置信息中,有一部分配置项是ODBC使用的,另一部分则由驱动程序处理。如果操作完全正确的话,现在ODBC已经成功了。可以试下isql命令操作刚配置的DSN。
# isql PosData -v
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>
注:-v参数是为了显示调试信息,便于出错时,显示信息方便调试。
代码测试 otl_test.cpp
#include <iostream>
using namespace std;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>#define OTL_ODBC_MYSQL // Compile OTL 4/ODBC/MySQL
#define OTL_ODBC_UNIX // uncomment this line if UnixODBC is used
#include <otlv4.h> // include the OTL 4 header fileotl_connect db; // connect object
void insert()
// insert rows into table
{
otl_stream o(1, // buffer size should be == 1 always on INSERT
"insert into test_tab values(:f1<int>,:f2<char[31]>)",
// SQL statement
db // connect object
);
char tmp[32];for(int i=1;i<=100;++i){
#if defined(_MSC_VER)
#if (_MSC_VER >= 1400) // VC++ 8.0 or higher
sprintf_s(tmp,sizeof(tmp),"Name%d",i);
#else
sprintf(tmp,"Name%d",i);
#endif
#else
sprintf(tmp,"Name%d",i);
#endif
o<<i<<tmp;
}
}void update(const int af1)
// insert rows into table
{
otl_stream o(1, // buffer size should be == 1 always on UPDATE
"UPDATE test_tab "
" SET f2=:f2<char[31]> "
" WHERE f1=:f1<int>",
// UPDATE statement
db // connect object
);o<<"Name changed"<<af1;
o<<otl_null()<<af1+1; // set f2 to NULL}
void select(const int af1)
{
// MyODBC does not allow any ih/nput bind variables in the WHERE clause
// in a SELECT statement.
// Therefore, the SELECT statement has to be generated literally.
char stmbuf[1024];
#if defined(_MSC_VER)
#if (_MSC_VER >= 1400) // VC++ 8.0 or higher
sprintf_s(stmbuf,sizeof(stmbuf),
"select * from test_tab where f1>=%d and f1<=%d*2",
af1,
af1
);
#else
sprintf(stmbuf,
"select * from test_tab where f1>=%d and f1<=%d*2",
af1,
af1
);
#endif
#else
sprintf(stmbuf,
"select * from test_tab where f1>=%d and f1<=%d*2",
af1,
af1
);
#endif
otl_stream i(50, // buffer size may be > 1
stmbuf, // SELECT statement
db // connect object
);
// create select stream
int f1;
char f2[31];while(!i.eof()){ // while not end-of-data
i>>f1;
cout<<"f1="<<f1<<", f2=";
i>>f2;
if(i.is_null())
cout<<"NULL";
else
cout<<f2;
cout<<endl;
}}
int main()
{
otl_connect::otl_initialize(); // initialize ODBC environment
try{//db.rlogon("UID=root;PWD=123456;DSN=test"); // connect to ODBC
db.rlogon("root/123456@test"); // connect to ODBC, alternative format
// of connect stringcout << "Connect to test successed" << endl;
// otl_cursor::direct_exec(db, "use test");
cout << "Exec use test succeed" << endl;otl_cursor::direct_exec
(
db,
"drop table test_tab",
otl_exception::disabled // disable OTL exceptions
); // drop tableotl_cursor::direct_exec
(
db,
"create table test_tab(f1 int, f2 varchar(30))"
); // create tableinsert(); // insert records into the table
update(10); // update records in the table
select(8); // select records from the table}
catch(otl_exception& p){ // intercept OTL exceptions
cerr << "Exception:" << endl;
cerr<<p.msg<<endl; // print out error message
cerr<<p.stm_text<<endl; // print out SQL that caused the error
cerr<<p.sqlstate<<endl; // print out SQLSTATE message
cerr<<p.var_info<<endl; // print out the variable that caused the error
}db.logoff(); // disconnect from ODBC
return 0;
}
g++ -o"otl_test" otl_test.cpp -lmyodbc3
(7) 测试结果
./mysql_otl
Connect to test successed
Exec use test succeed
f1=8, f2=Name8
f1=9, f2=Name9
f1=10, f2=Name changed
f1=11, f2=NULL
f1=12, f2=Name12
f1=13, f2=Name13
f1=14, f2=Name14
f1=15, f2=Name15
f1=16, f2=Name16
上面编译会出现两个错误:
问题一:
使用OTL连接访问mysql数据库编译提示sql.h sqlext.h头文件找不到
解决方法:安装unixodbc-dev
sudo apt-get install unixodbc-dev
问题二:
OTL连接myodbc报了一些undefined reference to `SQLFreeHandle 之类的链接错误,
解决方法:g++ -o"otl_test" otl_test.cpp -lodbc