安装相关系统包 ( in rocky linux)
sudo yum install epel-release
sudo yum install freetds freetds-devel unixODBC python3-pyodbc
# ubuntu
sudo apt-get install freetds-bin freetds-common tdsodbc odbcinst unixodbc unixodbc-dev
配置FREETDS
#sudo nano /etc/freetds.conf
[global]
[vmsap]
host =192.168.4.213
port = 1444
tds version = 7.0
# tds 版本根据对应的 sqlserver 版本选择,但需要测试
测试 freetds
tsql -S vmsap -U sa -P c123456 -p 1444
# TDSVER=7.0 tsql -S vmsap -U sa -P c123456 -p 1444
# /etc/odbcinst.ini
[FreeTDS]
Description=FreeTDS ODBC driver for SQL Server
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
FileUsage=1
配置 odbc
#sudo nano /etc/odbc.ini
[vmsap]
Driver = FreeTDS
Server = 192.168.4.213
Port = 1444
TDS_Version = 7.0
测试 odbc
isql -v vmsap sa c123456
测试代码 pyodbc
# -*- coding: utf-8 -*-
import pyodbc
import os
# 连接字符串
conn_str = (
"DRIVER={FreeTDS};"
"SERVER=192.168.4.213,1444;"
"UID=sa;"
"PWD=c123456;"
"DATABASE=master;"
"TDS_Version=7.0;"
)
# 连接SQL Server
conn = pyodbc.connect(conn_str)
print("连接成功!")
# 执行SQL查询
cursor = conn.cursor()
cursor.execute("SELECT @@VERSION")
row = cursor.fetchone()
print("SQL Server版本:", row[0])
# 关闭连接
conn.close()
print("连接已关闭")
网友评论