linux 下OTL连接SQLServer非配置版

来源:互联网 发布:蓝翔高级技工学校知乎 编辑:程序博客网 时间:2024/05/08 09:04

       在linux下连接sqlserver费了很多周折,关键是还不能配置,程序打包发出去就可以用,想想过了很多弯路,做次笔记也算是一些心得。


      参考:http://my.oschina.net/xuhh/blog/173968?fromerr=TcxY6YSC


      首先unixodbc和freetds是必须的,libiconv是一个转码的工具,可装可不装
     我的linux系统版本是centos7.1,连接sqlseerver版本是MSSQL 2008,unixodbc资源

      http://download.csdn.net/detail/mp295345033/9294067
      freetds 资源:

      http://share.weiyun.com/9cf07bfef09a8af69e8270cbda5058f7
      安装unixodbc,解压后进入文件夹,

      ./configure --enable-gui=no ; make ; sudo make install

      安装freetds:(不能在共享文件夹下安装,会报错!

      ./configure --with-tdsver=7.3 --with-unixodbc=/usr/local --with-libiconv-prefix=/usr/local --enable-msdblib;make;make install

      --with-tdsver: 是安装的版本,sqlserver必须指定7.3;

      --with-unixodbc:是unixodbc的安装路径;

      --with-libiconv-prefix:libconv的安装路径;

      --enable-msdblib:允许连接SQLServer;

      可能编译的时候还会有些错误,上网找找就可以解决了。

       安装完成就可以用官方的例子测试了

       

<span style="color:#333333;">#include <iostream>using namespace std;#include <stdio.h>#define OTL_ODBC_MSSQL_2008 // Compile OTL 4/ODBC, MS SQL 2005#define OTL_FREETDS_ODBC_WORKAROUNDS // Enable the FreeTDS / ODBC workarounds for MS SQL#define OTL_ODBC//#define OTL_ODBC // Compile OTL 4/ODBC. Uncomment this when used with MS SQL 7.0/ 2000#define OTL_ODBC_UNIX // Compile OTL 4 / ODBC. Uncomment this when used in Linux / Unix#define OTL_STL#include "otlv4.h"otl_connect db; // connect object int main(){ otl_connect::otl_initialize(); // initialize ODBC environment try{  db.rlogon("Driver=/usr/local/lib/libtdsodbc.so;Host=192.168.1.1;Port=1433;tds version = 7.3;Server=192.168.1.1;Database=DATE;Uid=sa;Pwd=password",1); // 此处为链接时候的选项,必须带1  otl_cursor::direct_exec   (    db,    "drop table test_tab",    otl_exception::disabled // disable OTL exceptions   ); // drop table  otl_cursor::direct_exec   (    db,    "create table test_tab(f1 int, f2 varchar(30))"    );  // create table  insert(); // insert records into table  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.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;}</span>


0 0