sql-server - 如何使用 .env 文件修复 sqlalchemy create_engine 的字符串格式?
问题描述
我正在尝试在虚拟环境中使用 python 将数据帧写入 sql server。我可以使用我的 pyodbc 连接从服务器读取数据,但无法使用该连接对其进行写入,因此我使用的是 sqlalchemy 引擎,并且凭据存储在 .env 文件中。
打印原始字符串返回:
'mssql+pyodbc://User:Password@Server/Database?trusted_connection=no&driver=ODBC+Driver+17+for+SQL+Server'
但是在打印引擎时它会返回:
'mssql+pyodbc://User:***sword@Server/Database?trusted_connection=no&driver=ODBC+Driver+17+for+SQL+Server'
如果我尝试使用引擎进行连接,登录超时将过期,我假设这是因为引擎没有传递正确的凭据。
我的字符串格式有什么问题吗?
import os
from sqlalchemy import create_engine
from dotenv import load_dotenv
load_dotenv()
credentials = [os.getenv('UID'), os.getenv('PWD'), os.getenv('Server'), os.getenv('Database')]
engine = create_engine('mssql+pyodbc://{0}:{1}@{2}/{3}?trusted_connection=no&driver=ODBC+Driver+17+for+SQL+Server'.format(*credentials),fast_executemany=True)
print('mssql+pyodbc://{0}:{1}@{2}/{3}?trusted_connection=no&driver=ODBC+Driver+17+for+SQL+Server'.format(*credentials))
print(engine)
engine.connect()
解决方案
推荐阅读
- javascript - 数据生成中处理html创建(服务器端或客户端)的最佳位置是什么
- javascript - 我如何计算得到第二个 div 宽度和高度的厘米?
- python - TypeError: '(slice(None, None, None), slice(1, None, None))' is an invalid key
- azure - 如何从 yaml 和 jq 读取从 github 复制的文件
- python-3.x - 如何使用 Cognito 进行 AppSync 变异调用(Python)
- html - 如何通过 HTML 表单将图像直接提交/发布到 TinEye?
- node.js - 无法在 Windows 10 上安装 node express
- java - 如何配置 Kudu 测试工具以避免“块缓存容量超过内存压力阈值”
- c++ - 从 lambda 中的空指针调用方法
- linux - 即使在配置时提供了路径,Cmake 也找不到文件