You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Polars将DataFrame写入SQLite数据库时触发OperationalError

使用Polars将DataFrame写入SQLite时出现OperationalError的解决方法

问题场景

尝试用Polars把DataFrame写入SQLite数据库,初始代码如下:

import polars as pl
import sqlite3

conn = sqlite3.connect("test.db")
df = pl.DataFrame({"col1": [1, 2, 3]})

按照pl.write_database的要求传入SQLite连接URI字符串后,执行代码:

df.write_database("test_table", f"sqlite:////test.db", if_table_exists="replace")

触发以下错误:

OperationalError: (sqlite3.OperationalError) unable to open database file

解决步骤

  • 执行命令安装所需依赖:
pip install polars[sqlalchemy]

原因说明

Polars的write_database方法依赖SQLAlchemy处理数据库连接逻辑(包括解析SQLite的URI格式)。未安装该依赖时,无法正确识别URI形式的连接字符串,进而导致数据库文件无法打开。

内容的提问来源于stack exchange,提问作者Péter Szilvási

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 22:07:08