gpt4 book ai didi

python - Python 中 Sqlite 查询提供的绑定(bind)数量不正确

转载 作者:太空宇宙 更新时间:2023-11-03 15:39:18 25 4
gpt4 key购买 nike

在 python 中运行以下代码时,出现错误:提供的绑定(bind)数量不正确。当前语句使用 0,并且提供了 1。对于查询语句。

conn = sqlite3.connect('viber_messages2')

cur = conn.cursor()
cur = cur.execute("""SELECT DISTINCT messages.conversation_id
FROM messages
INNER JOIN participants ON messages.participant_id = participants._id
WHERE messages.conversation_id IS NOT NULL;""")

query = ("""SELECT messages._id, messages.body, messages.conversation_id, messages.participant_id, participants_info.number, participants_info.contact_name
FROM messages
INNER JOIN
participants ON messages.participant_id = participants._id
INNER JOIN
participants_info ON participants.participant_info_id = participants_info._id;""")

with open('messages.html', 'w') as h, open('test.txt', 'w') as t:
for convo in cur.fetchall():
df = pd.read_sql_query(query, conn, params=convo)

# HTML WRITE
h.write(df.to_html())
h.write('<br/>')

# TXT WRITE
t.write(df.to_string())
t.write('\n\n')

cur.close()
conn.close()

最佳答案

让我们仔细看看您的查询

SELECT messages._id, messages.body, messages.conversation_id, messages.participant_id, participants_info.number, participants_info.contact_name
FROM messages
INNER JOIN
participants ON messages.participant_id = participants._id
INNER JOIN
participants_info ON participants.participant_info_id = participants_info._id

这里没有可以绑定(bind)参数的占位符。但是你如何执行这个查询呢?

df = pd.read_sql_query(query, conn, params=convo)

你应该这样做

df = pd.read_sql_query(query, conn)

关于python - Python 中 Sqlite 查询提供的绑定(bind)数量不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42295857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com