LINUX下通過C 訪問SQLSERVER資料庫

2021-07-05 20:42:08 字數 1377 閱讀 4066

linux下通過c++訪問微軟的資料庫,有點麻煩。微軟官方的linux資料庫驅動支援不好,本例通過unixodbc,使用freetds驅動,再通過soci封裝的odbc方式訪問資料庫(具體版本:unixodbc-2.3.4.tar.gz和freetds-0.95.19.tar.gz)

tar -xvf unixodbc-2.3.0.tar.gz

./configure --prefix=/usr --libdir=/usr/lib64 --sysconfdir=/etc --enable-gui=no --enable-drivers=no --enable-iconv=yes --with-iconv-char-enc=gb18030 --with-iconv-ucode-enc=utf16le

make

make install

tar -xvf freetds-patched.tar.gz

configure

make

make install

安裝完成後,用isql命令來驗證是否能成功連線到sql servcer資料庫,一定要這一步驗證了後,才能繼續編碼開發

tar -xvf soci-3.2.2.tar.gz

cmake -g "unix makefiles" -dcmake_install_prefix=./inss -dwith_boost=off -dwith_mysql=off -dwith_oracle=off -dwith_postgresql=off -dwith_sqlite3=off -dwith_firebird=off -dwith_db2=off

make

make install

如果有問題或者直接報錯,記得加上odbc的日誌

odbcinst.ini檔案中增加:

[odbc]

tracefile = /tmp/sql.log

trace = yes

如果報錯如下,可以再freetds配置中加上字元編碼

diag [hy000] [freetds][sql server]error converting characters into server's character set. some character(s) could not be converted

配置freetds:/usr/local/etc/freetds.conf,增加: 

client charset = gb18030

一、微軟官方的驅動不能用,除了中文亂碼外,還斷斷續續的不穩定

二、freetds版本0.95.0存在記憶體洩露,換用0.95.19解決問題

三、寫入資料庫時,字串會多一位(ascii為0),需要修改backends/odbc/vector-use-type.cpp檔案中// add one for null注釋位置的**

Linux下C 訪問MySQL《一》

配置開發環境 首先需要編譯 安裝mysql,安裝完成後,將mysql目錄中的lib目錄新增到環境變數中。新建c c 工程,把 mysql root include新增到編譯環境的包含路徑下面。在編譯選項中,增加 mysql root lib目錄。在link選項中增加 lmysqlclient 已經把...

linux通過c語言程式設計訪問遠端mysql

cpp view plain copy include include include mysql mysql.h intmain void else char sql select host,user from user order by rand if mysql query mysql,sql...

windows下通過vnc遠端訪問ubuntu

通過將伺服器配置成vnc server,可以讓其他主機使用圖形方式登入這台伺服器。在ubuntu下配置vnc server很簡單,方法如下 伺服器端 1 先安裝vnc服務程式 sudo apt get install vnc4 common vnc4server 2 給當前使用者設定vnc登入密碼 ...