2017-02-17 61 views
1

在python中運行以下代碼時,出現錯誤:提供的綁定數量不正確。當前語句使用0,並提供1。對於查詢語句。在Python中提供的綁定數量不正確Sqlite查詢

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() 

回答

1

讓我們在查詢的密切關注

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 

有沒有佔位符,在這裏它的參數可以綁定。但你如何執行這個查詢?

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

而應該做

df = pd.read_sql_query(query, conn) 
+0

啊,是我的錯!它修復了錯誤,但是現在我的代碼不再將結果拆分爲基於消息表中的conversation_id的單獨表。 – Nathan1995

+0

我修復它的劃痕!謝謝! – Nathan1995

相關問題