feat:使用pymssql替换pyodbc

master
old-tom 2 years ago
parent f0bf23da6f
commit 5e26a06389

@ -16,4 +16,24 @@ pool_size = 15
# 连接超时回收(秒)
pool_recycle = 3600
# 打印SQL
show_sql = false
show_sql = false
[credit]
# 方言
dialect = 'sqlserver'
# ip
host = '172.16.1.2'
# 端口
port = 1433
# 用户名
user = 'sa'
# 密码
passwd = 'atdPriME@Gy9#'
# 数据库
database = 'credit'
# 连接池大小
pool_size = 15
# 连接超时回收(秒)
pool_recycle = 3600
# 打印SQL
show_sql = true

@ -8,7 +8,24 @@
from session.engine.dbengine import DatabaseEngine
if __name__ == '__main__':
db_engine = DatabaseEngine('jtt_crm')
db_engine = DatabaseEngine('credit')
session_factory = db_engine.create_session_factory()
sess = session_factory.open_session()
print(sess.select_all('select * from JTXY_CREDIT_TASK'))
print(sess.select_all('select * from hat_area'))
# import pyodbc
# # Some other example server values are
# # server = 'localhost\sqlexpress' # for a named instance
# # server = 'myserver,port' # to specify an alternate port
# server = '172.16.1.2'
# database = 'credit'
# username = 'sa'
# password = 'atdPriME@Gy9#'
# # ENCRYPT defaults to yes starting in ODBC Driver 18. It's good to always specify ENCRYPT=yes on the client side to avoid MITM attacks.
# cnxn = pyodbc.connect(DRIVER='ODBC Driver 18 for SQL Server', SERVER=server, DATABASE=database, UID=username, PWD=password)
# cursor = cnxn.cursor()
# cursor.execute("SELECT TOP 10 * FROM ICSTOCKBILL")
# row = cursor.fetchone()
# while row:
# print(row)
# row = cursor.fetchone()

@ -0,0 +1,55 @@
# 一、在m1 mac下使用pyodbc (大坑需要修改tsl协议版本不建议使用)
参考官方文档 https://github.com/mkleehammer/pyodbc/wiki
```shell
# 1.安装odbc
brew update
brew install unixodbc
# 2.安装sqlserver linux驱动
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
HOMEBREW_ACCEPT_EULA=Y brew install msodbcsql18 mssql-tools18
# 3.安装pyodbc
pip install --no-binary :all: pyodbc
```
```shell
# 查看配置文件路径
odbcinst -j
# 输出
unixODBC 2.3.12
DRIVERS............: /opt/homebrew/etc/odbcinst.ini
SYSTEM DATA SOURCES: /opt/homebrew/etc/odbc.ini
FILE DATA SOURCES..: /opt/homebrew/etc/ODBCDataSources
USER DATA SOURCES..: /Users/old-tom/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
# 修改odbcinst.ini
[ODBC Driver 18 for SQL Server]
Description=Microsoft ODBC Driver 18 for SQL Server
Driver=/opt/homebrew/lib/libmsodbcsql.18.dylib
```
# 二、在m1 mac下使用pymssql 连接sqlserver
参考官方文档 https://pymssql.readthedocs.io/en/stable/freetds.html
```shell
# 1.安装freetds
brew install freetds
# 2.修改配置
vim /opt/homebrew/etc/freetds.conf
在[global]下添加
port = 1443
tds version = 7.0
# 3.测试连接
tsql -H 172.16.1.2 -p 1433 -U sa -P atdPriME@Gy9# -D credit
# 返回以下情况说明连接成功
locale is "zh_CN.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
Setting credit as default database in login packet
```
freetds连接成功后安装pymssql
```shell
pip install pymssql
```

@ -7,6 +7,7 @@ psycopg2-binary==2.9.6
pycparser==2.21
pydantic==2.0.2
pydantic_core==2.1.2
pymssql==2.2.8
SQLAlchemy==2.0.7
toml==0.10.2
typing_extensions==4.7.1

@ -54,7 +54,7 @@ class DatabaseEngine(object):
'postgresql': 'postgresql+psycopg2://{0}:{1}@{2}:{3}/{4}',
# 使用thin客户端,不需要oracle client
'oracle': 'oracle+oracledb://{0}:{1}@{2}:{3}/?service_name={4}',
# arm下没连接成功,需要x86版本python
# pyodbc 不支持m1这里使用pymssql
'sqlserver': 'mssql+pymssql://{0}:{1}@{2}/{4}?charset=CP936'
}
# 连接池容器名称,具体看ObjContainer对象

Loading…
Cancel
Save