From 5e26a0638963f5b584cb68aa72723d04c37aac3a Mon Sep 17 00:00:00 2001 From: old-tom <892955278@msn.cn> Date: Thu, 24 Aug 2023 11:40:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E4=BD=BF=E7=94=A8pymssql=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2pyodbc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db-conf.toml | 22 ++++++++++++++- main.py | 21 +++++++++++++-- readme.md | 55 ++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + session/engine/dbengine.py | 2 +- 5 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 readme.md diff --git a/db-conf.toml b/db-conf.toml index c5dc1ab..feddc10 100644 --- a/db-conf.toml +++ b/db-conf.toml @@ -16,4 +16,24 @@ pool_size = 15 # 连接超时回收(秒) pool_recycle = 3600 # 打印SQL -show_sql = false \ No newline at end of file +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 \ No newline at end of file diff --git a/main.py b/main.py index 95dadeb..39a63fd 100644 --- a/main.py +++ b/main.py @@ -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() diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..589fb7a --- /dev/null +++ b/readme.md @@ -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 +``` \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 28516c2..bedcb14 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/session/engine/dbengine.py b/session/engine/dbengine.py index c4d5e6a..fe35aad 100644 --- a/session/engine/dbengine.py +++ b/session/engine/dbengine.py @@ -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对象