2013-06-04 36 views
10

Vì vậy, tôi gặp khó khăn lớn khi cả ba đều làm việc cùng nhau trong sự hài hòa. Tôi đoán tôi sẽ liệt kê tất cả các cấu hình khác nhau, cùng với mã kiểm tra để xem một cặp mắt mới có thể nhận ra sự ngu ngốc của tôi không.Có Rắc rối với UnixODBC, FreeTDS và PyODBC

Tôi đang chạy 12.04 Máy chủ Unbuntu và tôi đang cố gắng kết nối với máy chủ MSSQL 2008 và kết thúc bằng cách sử dụng nó với PyODBC.

Tuy nhiên, khi chỉ cần đặt trong

tsql -S T2 -U Foo -P Bar 

tôi nhận được

1> 
2> 
3> 
4> 
5> 
6> 
7> 
8> 
9> 
10> 
11> 

và vv

Dù sao, nếu có ai sẽ có thể giúp (và tôi sẽ mãi mãi biết ơn nếu bạn có thể xóa tôi khỏi đám mây này), đây là các cấu hình hiện tại của tôi.

Đây là /etc/odbc.ini tôi

[ODBC Data Sources] 
odbcname  = MySQL 
T2   = MSSQL 

[odbcname] 
Driver  = /usr/lib/odbc/libmyodbc.so 
Description = MyODBC 3.51 Driver DSN 
SERVER  = Foobar 
PORT   = 3306 
USER   = Foo 
Password  = Bar 
Database  = Foobar 
OPTION  = 3 
SOCKET  = 

[T2] 
Driver  = FreeTDS 
Description = ODBC connection via FreeTDS 
SERVER  = FOOBAR 
PORT   = 1433 
USER   = Foo 
Password  = Bar 
Database  = Foobar 
OPTION  = 3 
SOCKET  = 

[Default] 
Driver  = /usr/local/lib/libmyodbc3.so 
Description = MyODBC 3.51 Driver DSN 
SERVER  = FOOBAR 
PORT   = 3306 
USER   = foo 
Password  = bar 
Database  = FOOBAR 
OPTION  = 3 
SOCKET  = 

Sau đây là tôi /etc/odbcinst.ini

[FreeTDS] 
Description=FreeTDS Driver 
Driver=/usr/lib/odbc/libtdsodbc.so 
Setup=/usr/lib/odbc/libtdsS.so 
CPTimeout= 
CPReuse= 
FileUsage=1 

Sau đây là freetds.conf tôi

# This file is installed by FreeTDS if no file by the same 
# name is found in the installation directory. 
# 
# For information about the layout of this file and its settings, 
# see the freetds.conf manpage "man freetds.conf". 

# Global settings are overridden by those in a database 
# server specific section 
[global] 
     # TDS protocol version 
;  tds version = 4.2 

     # Whether to write a TDSDUMP file for diagnostic purposes 
     # (setting this to /tmp is insecure on a multi-user system) 
;  dump file = /tmp/freetds.log 
;  debug flags = 0xffff 

     # Command and connection timeouts 
;  timeout = 10 
;  connect timeout = 10 

     # If you get out-of-memory errors, it may mean that your client 
     # is trying to allocate a huge buffer for a TEXT field. 
     # Try setting 'text size' to a more reasonable limit 
     #text size = 64512 

[T2] 
     host = FOOBAR 
     port = 1433 
     tds version = 7.0 
     client charset = UTF-8 
     text size = 20971520 
[global] 
     # TDS protocol version 
     tds version = 7.0 

Và tệp thử nghiệm Python của tôi chỉ để đo lường tốt

import pyodbc 
import sys 

try: 
    #tempsystrends = pyodbc.connect('DRIVER=FreeTDS;SERVER=FOOBAR;PORT=1433;DATABASE=T2;UID=FOO;PWD=bar;TDS_Version=7.0;') 
    cursor = tempsystrends.cursor() 
except pyodbc.Error as e: 
     print "Error: %s" % (e.args[1]) 
     sys.exit(1) 

Trả lời

13

tôi kết nối với cơ sở dữ liệu khác nhau thông qua PHP sử dụng UnixODBC, đây là cấu hình của tôi cho một Microsoft SQL Server:

/etc/odbc.ini

# Define a connection to a Microsoft SQL server 
# The Description can be whatever we want it to be. 
# The Driver value must match what we have defined in /etc/odbcinst.ini 
# The Database name must be the name of the database this connection will connect to. 
# The ServerName is the name we defined in /etc/freetds/freetds.conf 
# The TDS_Version should match what we defined in /etc/freetds/freetds.conf 
[mssql] 
Description    = MSSQL Server 
Driver     = freetds 
Database    = XXXXXX 
ServerName    = MSSQL 
TDS_Version    = 8.0 

/etc/odbcinst.ini

# Define where to find the driver for the Free TDS connections. 
[freetds] 
Description  = MS SQL database access with Free TDS 
Driver   = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so 
Setup   = /usr/lib/i386-linux-gnu/odbc/libtdsS.so 
UsageCount  = 1 

/etc/freetds/freetds.conf

# The basics for defining a DSN (Data Source Name) 
# [data_source_name] 
#  host = <hostname or IP address> 
#  port = <port number to connect to - probably 1433> 
#  tds version = <TDS version to use - probably 8.0> 

# Define a connection to the Microsoft SQL Server 
[mssql] 
     host = XXXXXX 
     port = 1433 
     tds version = 8.0 

Sau đó kiểm tra kết nối của bạn:

isql mssql username password 

Tùy thuộc vào môi trường của bạn của bạn username có thể có được trong các định dạng: domain \ username

Sau khi ra lệnh, bạn sẽ thấy một cái gì đó như:

+---------------------------------------+ 
| Connected!       | 
|          | 
| sql-statement       | 
| help [tablename]      | 
| quit         | 
|          | 
+---------------------------------------+ 
SQL> 
+2

WOW. Đây có lẽ là câu trả lời hoàn chỉnh nhất mà tôi từng đọc trên Stack Overflow. Chỉ có thay đổi đối với tôi là thay đổi vị trí cho 64bit trong /etc/odbcinst.ini: /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so – philshem

+1

@philshem - cảm ơn, tôi rất vui vì nó hữu ích! –

+2

Câu trả lời hoàn chỉnh nhất cho đến nay! – digz6666

Các vấn đề liên quan