問題描述
我正在嘗試使用 pyodbc 連接到 Microsoft sql server 數(shù)據(jù)庫(kù).我不斷收到錯(cuò)誤
I am trying to connect to a Microsoft sql server database using pyodbc. I keep getting the error
錯(cuò)誤: ('01000', "[01000] [unixODBC][Driver Manager]無(wú)法打開 lib 'ODBC Driver 17 for SQL Server': 找不到文件 (0) (SQLDriverConnect)")>
Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")
檢查 pyodbc.drivers()
沒有結(jié)果
我根據(jù)提供的說(shuō)明安裝了 Microsoft ODBC 驅(qū)動(dòng)程序 這里:
I installed the Microsoft ODBC driver according to the instructions provided here:
我運(yùn)行了 odbcinst -j
產(chǎn)生了
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /Users/pawannandakishore/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
但是當(dāng)我到達(dá) /etc
時(shí),我找不到 odbcinst.ini
或 odbc.ini
.他們似乎在 opt/homebrew/Cellar/
but when I got to /etc
, I cannot find either odbcinst.ini
or odbc.ini
. They are seem to be in opt/homebrew/Cellar/
我真的很感激這方面的幫助.
I would really appreciate some help on this.
推薦答案
UPD:
您實(shí)際上可以在容器中使用 pyodbc
但容器應(yīng)該在 x86_64 架構(gòu)容器中構(gòu)建和執(zhí)行.為此,您需要將 platform
添加到 docker-compose.yml
或在容器運(yùn)行期間(使用 docker 時(shí))提供參數(shù).您需要確保使用 buildx 構(gòu)建容器!
UPD:
You actually can use pyodbc
in the container but the container should be built and executed in the x86_64 arch container. In order to do this, you need to add the platform
either to docker-compose.yml
or provide an argument during container run (when using docker).
You need to make sure that you are building a container using buildx!
碼頭工人:
docker buildx build --platform linux/amd64 -t myimage .
docker run --platform linux/amd64 myimage bash
Docker-compose:
Docker-compose:
version: "3.9"
services:
web:
image: myimage:latest
build:
context: ./
platform: linux/amd64
原答案:
pyodbc
確實(shí)不支持 ARM 架構(gòu).我在 M1 上使用 pymssql
以連接到 MSSQL 服務(wù)器.
Original answer:
It's true that pyodbc
does not support ARM architecture. I'm using pymssql
on my M1 in order to connect to MSSQL server.
您需要安裝系統(tǒng)要求
freetds-dev
.對(duì)于 alpine 容器,它將是apk add freetds-dev
You need to install the system requirement
freetds-dev
. For alpine container, it will beapk add freetds-dev
在 pip 要求中添加 pymssql
包.
In pip requirements add pymssql
package.
使用簡(jiǎn)單腳本測(cè)試連接:
Test connection with simple script:
import pymssql
conn = pymssql.connect(server='mssql', user='SA', password='Passw@rd', database='master')
cursor = conn.cursor()
cursor.execute("""SELECT 1;""")
- SqlAlchemy 連接應(yīng)該如下所示
SQLALCHEMY_DATABASE_URI = (
f"mssql+pymssql://{MSSQL_USER}:{MSSQL_PASSWORD}@{MSSQL_HOST}/{MSSQL_DB}?"
)
如果您需要在 M1 上運(yùn)行 MSSQL 數(shù)據(jù)庫(kù) - 這是我對(duì)這個(gè)問題的回答 https://stackoverflow.com/a/66919852/11515610
If you need to run MSSQL database on M1 - here my answer on this problem https://stackoverflow.com/a/66919852/11515610
相關(guān)鏈接:
自適應(yīng)服務(wù)器連接失敗(DB-Lib 錯(cuò)誤消息 20002,嚴(yán)重性 9)
https://docs.sqlalchemy.org/en/14/dialects/mssql.html#module-sqlalchemy.dialects.mssql.pymssql
https://docs.microsoft.com/en-us/sql/connect/python/pymssql/step-1-configure-development-environment-for-pymssql-python-development?view=sql-server-ver15
https://pymssql.readthedocs.io/en/stable/pymssql_examples.html
這篇關(guān)于M1 Mac 上的 Pyodbc的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!