sqlite 导入错误:使用未安装sqlalchemy的URI字符串,同时使用SqlLite在panda SQL API上执行REGEXP函数

dxxyhpgq  于 2022-12-13  发布在  SQLite
关注(0)|答案(2)|浏览(471)

我正在尝试使用Pandas SQL API执行SqlLite的REGEXP函数,但出现以下错误
“汇入错误:使用未安装sqlalchemy的URI字符串。“ohon
python代码如下:

import pandas as pd
import csv, sqlite3
import json, re

conn = sqlite3.connect(":memory:")

print(sqlite3.version)
print(sqlite3.sqlite_version)

def regexp(y, x, search=re.search):
    return 1 if search(y, x) else 0

conn.create_function("regexp", 2, regexp)
df = pd.read_json("idxData1.json", lines=True)
df.to_sql("temp_log", conn, if_exists="append", index=False)

rsDf = pd.read_sql_query(
    conn, """SELECT * from temp_log WHERE user REGEXP 'ph'""", chunksize=20,
)

for gendf in rsDf:
    for item in gendf.to_dict(orient="records"):
        print(item)

它抛出的错误是

raise ImportError("Using URI string without sqlalchemy installed.")
ImportError: Using URI string without sqlalchemy installed.

有没有人能建议我缺少什么。请不要说我有一个使用PandasSQL API的具体要求。

pbwdgjma

pbwdgjma1#

出现此错误的原因是您以错误的顺序指定了read_sql_query的参数。具体来说,第一个参数应该是查询,第二个参数应该是连接,如下所示:

rsDf = pd.read_sql_query(
    """SELECT * from temp_log WHERE user REGEXP 'ph'""", conn, chunksize=20,
)
mrwjdhj3

mrwjdhj32#

您只需运行以下命令并安装SQLAlchemy

pip3 install SQLAlchemy

正如@Xbel所说:
请注意,引发该错误的原因可能是参数的顺序错误,例如,先添加连接,然后添加SQL语句。看起来确实如此。请注意,安装SQLAlchemy并没有帮助。

相关问题