[英]Problems with Trac post commit script (SyntaxError: invalid syntax)
[英]SyntaxError: invalid syntax on commit
import sqlite3
import numpy
conn = sqlite3.connect('lotto.db')
cur = conn.cursor()
def fun(a,b,c,d,e,f):
list = [a, b, c ,d, e, f]
print(list)
return numpy.mean(list)
numbers = cur.execute("SELECT * FROM combinations6")
numbers.fetchall()
for row in numbers:
cur.execute("UPDATE combinations6 WHERE id = ? SET average = ?", (row, fun(row[0],row[1],row[2],row[3],row[4],row[5]))
conn.commit()
conn.close()
在遍歷每行中遇到語法錯誤時遇到麻煩,當它運行時,它僅計算第一行的平均值並將其輸入數據庫的所有行
我要怎么做才能遍歷每一行並計算平均值並將其輸入數據庫?
python非常新,因此在此先感謝您的幫助。
問題不在於Python,而是SQL語法。 WHERE
子句位於SET
之后 :
cur.execute("UPDATE combinations6 SET average = ? WHERE id = ?", (fun(row[0],row[1],row[2],row[3],row[4],row[5]), row)
不要忘記交換替換參數的順序來匹配它。
另外,您使用row
作為id = ?
的參數id = ?
,這是不對的。 您不能將整個列表放入參數中,它必須是列表的特定元素,例如row[6]
類的東西。 我不知道表中ID列的實際位置,所以我不知道正確的索引是什么。
您還可以通過單個查詢來完成整個操作:
UPDATE combinations6
SET average = (col1 + col2 + col3 + col4 + col5 + col6)/5
用要計算平均值的列的實際名稱替換col1
等。
import sqlite3
import numpy
conn = sqlite3.connect('lotto.db')
cur = conn.cursor()
def fun(a,b,c,d,e,f):
list = [a, b, c ,d, e, f]
print(list)
return numpy.mean(list)
numbers = cur.execute("SELECT * FROM combinations6")
num = numbers.fetchall()
for row in num:
cur.execute("UPDATE combinations6 SET average = ? WHERE id = ?", (fun(row[0],row[1],row[2],row[3],row[4],row[5]), row[7]))
conn.commit()
conn.close()
奇怪地修復了添加到查詢並使用不同的指針和不同的查詢
num = numbers.fetchall()
感謝您的協助,讓我到達這里:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.