[英]Unable to use pydobc to connect to SQL Server using pyodbc in Python 3.6 in AWS Lambda
I have been struggling, and am unable to figure out how to connect to SQL Server using python 3.6 via pydobc and AWS Lambda. 我一直在努力,无法弄清楚如何通过pydobc和AWS Lambda使用python 3.6连接到SQL Server。
I followed the instructions provided by AWS to create an Amazon Linux AMI on which I was able to install the Microsoft ODBC drivers (v13 and v17), upgrade the unixODBC to a supported version, and get my python code to connect to the AWS RDS SQL Server instance. 我按照AWS提供的说明创建了一个Amazon Linux AMI,可以在其上安装Microsoft ODBC驱动程序(v13和v17),将unixODBC升级到受支持的版本,并获取我的python代码以连接到AWS RDS SQL服务器实例。
However, I have not been able to figure out how to package those changes successfully to deploy this code to AWS Lambda and have it work. 但是,我无法弄清楚如何成功打包这些更改以将该代码部署到AWS Lambda并使其正常工作。
I get one of two errors, depending on how I try to reference the ODBC driver. 我收到两个错误之一,具体取决于我如何尝试引用ODBC驱动程序。 Using this syntax 使用此语法
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=servername.account.region.rds.amazonaws.com,port;DATABASE=database;UID=user;PWD=password'
I get the error: 我得到错误:
"errorMessage": "('01000', \"[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)\")",
I have tried using other driver aliases (ODBC Driver 17 for SQL Server, ODBC Driver 13 for SQL Server), with the same results. 我尝试使用其他驱动程序别名(用于SQL Server的ODBC驱动程序17,用于SQL Server的ODBC驱动程序13),结果相同。
Using the syntax: 使用语法:
cnxn = pyodbc.connect('DRIVER=lib/libmsodbcsql-13.so;SERVER=servername.account.region.rds.amazonaws.com,port;DATABASE=database;UID=user;PWD=password')
I get the error: 我得到错误:
'IM004', "[IM004] [unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed (0) (SQLDriverConnect)")
I have the following code in my .ZIP deployment file: 我的.ZIP部署文件中包含以下代码:
I've toyed around with directories, and adding more libodbc*.* files from the /usr/lib64 directory, but nothing has worked so far. 我弄弄了目录,并从/ usr / lib64目录中添加了更多libodbc *。*文件,但到目前为止没有任何效果。 As well as bringing over the entire msodbcsql directory (with /etc, /include, /lib64, and /share). 以及带来整个msodbcsql目录(带有/ etc,/ include,/ lib64和/ share)。
Any help would be greatly appreciated! 任何帮助将不胜感激!
Based in this answer: AWS Lambda function to connect to SQL Server with Python 基于此答案: AWS Lambda函数可使用Python连接到SQL Server
You will need to compile unixODBC, take its shared libraries. 您将需要编译unixODBC,并使用其共享库。 Then install the MS driver, takes its shared libs and gather together 然后安装MS驱动程序,获取其共享库并收集在一起
# Start a container that mimic the lambda environment
docker run -it --rm --entrypoint bash -e ODBCINI=/var/task -e ODBCSYSINI=/var/task -v "$PWD":/var/task lambci/lambda:build-python2.7
# Then, download ODBC source code, compile and take the output
curl ftp://ftp.unixodbc.org/pub/unixODBC/unixODBC-2.3.5.tar.gz -O
tar xvzf unixODBC-2.3.5.tar.gz
cd unixODBC-2.3.5
./configure --sysconfdir=/var/task --disable-gui --disable-drivers --enable-iconv --with-iconv-char-enc=UTF8 --with-iconv-ucode-enc=UTF16LE --prefix=/home
make install
cd ..
mv /home/* .
mv unixODBC-2.3.5 unixODBC-2.3.5.tar.gz /tmp/
# Install MSsql odbc driver
curl https://packages.microsoft.com/config/rhel/6/prod.repo > /etc/yum.repos.d/mssql-release.repo
ACCEPT_EULA=Y yum -y install msodbcsql
export CFLAGS="-I/var/task/include"
export LDFLAGS="-L/var/task/lib"
# Then you can install pyodbc (or pip install -t . -r requirements.txt)
pip install pyodbc -t .
cp -r /opt/microsoft/msodbcsql .
cat <<EOF > odbcinst.ini
[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server
Driver=/var/task/msodbcsql/lib64/libmsodbcsql-13.1.so.9.2
UsageCount=1
EOF
cat <<EOF > odbc.ini
[ODBC Driver 13 for SQL Server]
Driver = ODBC Driver 13 for SQL Server
Description = My ODBC Driver 13 for SQL Server
Trace = No
EOF
# Test if it works
python -c "import pyodbc; print(pyodbc.drivers());"
python -c 'import pyodbc;conn = pyodbc.connect("DRIVER={ODBC Driver 13 for SQL Server}; SERVER=YOUr_SERVER:ADD;PORT=1443;DATABASE=TestDB;UID=SA;PWD=<YourStrong!Passw0rd>");crsr = conn.cursor();rows = crsr.execute("select @@VERSION").fetchall();print(rows);crsr.close();conn.close()'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.