预期 URI 为字符串 to_sql()

问题描述 投票:0回答:2

我正在尝试将大型 dask 数据帧上传到 sql 服务器,但出现错误,并且看不到连接字符串有任何问题。我可以使用此连接字符串连接到数据库,但在运行以下命令时出现错误:

import sqlalchemy as sa
import dask.dataframe as dd
from dask.diagnostics import ProgressBar
pbar = ProgressBar()
pbar.register()
#windows authentication + fast_executemany=True
to_sql_uri = sa.create_engine('mssql+pyodbc://TEST-BI/DB_TEST?driver=SQL Server?Trusted_Connection=yes', fast_executemany=True)
ddf.to_sql('test', uri=to_sql_uri, if_exists='replace', index=False)

ValueError:预期 URI 为字符串,得到

我尝试强制将 to_sql_uri 转换为字符串,但仍然出现错误。

python sqlalchemy dask dask-to-sql
2个回答
2
投票

如果文档不够清晰,我很抱歉(文档页面api参考)。但是,您确实没有在调用中传递了字符串,而是传递了一个引擎实例,这就是错误消息所说的内容。

你应该这样做

ddf.to_sql('test', 
    uri='mssql+pyodbc://TEST-BI/DB_TEST?driver=SQL Server?Trusted_Connection=yes', 
    if_exists='replace', index=False)

0
投票

只是让您知道

dask.dataframe.to_sql
现在(至少版本 2023.12.1)接受
engine_kwargs
参数(Pull Request #8609),可用于为底层 SQLAlchemy 引擎提供参数。因此,如果您想将
dask.dataframe.to_sql
fast_executemany
驱动程序选项
一起使用,您现在可以执行以下操作:

ddf.to_sql('test', 
    uri='mssql+pyodbc://TEST-BI/DB_TEST?driver=SQL Server?Trusted_Connection=yes', 
    if_exists='replace', index=False, engine_kwargs={"fast_executemany": True})
© www.soinside.com 2019 - 2024. All rights reserved.