Python SyntaxError:提交时语法无效
如果无法让它在每一行上迭代,就会出现语法错误,并且当它确实运行时,它只计算第一行的平均值,并将其输入到数据库的所有行 让它遍历每一行并计算平均值并将其输入到数据库中,我做错了什么Python SyntaxError:提交时语法无效,python,sqlite,Python,Sqlite,如果无法让它在每一行上迭代,就会出现语法错误,并且当它确实运行时,它只计算第一行的平均值,并将其输入到数据库的所有行 让它遍历每一行并计算平均值并将其输入到数据库中,我做错了什么 python非常新,因此提前感谢您的帮助。问题不在于python,而在于您的SQL语法。WHERE子句位于SET之后: import sqlite3 import numpy conn = sqlite3.connect('lotto.db') cur = conn.cursor() def fun(a,b,c
python非常新,因此提前感谢您的帮助。问题不在于python,而在于您的SQL语法。
WHERE
子句位于SET
之后:
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()
不要忘记交换替换参数的顺序以匹配此项
另外,您正在使用行
作为id=?
的参数,这是不对的。您不能将整个列表放入参数中,它必须是列表的特定元素,例如类似于行[6]
。我不知道ID列在表中的实际位置,所以我不知道正确的索引是什么
您还可以通过一个查询完成整个过程:
cur.execute("UPDATE combinations6 SET average = ? WHERE id = ?", (fun(row[0],row[1],row[2],row[3],row[4],row[5]), row)
将col1
等替换为要计算平均值的列的实际名称
UPDATE combinations6
SET average = (col1 + col2 + col3 + col4 + col5 + col6)/5
奇怪地修复了添加到查询并使用不同指针和不同查询的问题
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()
感谢您帮助我完成以下任务:)将
行
参数替换为包含ID的列表元素。或者只使用我添加到答案中的新查询。不要更改问题以删除问题。如果要显示新版本的代码,请将其添加为附加内容。
num = numbers.fetchall()