简体   繁体   English

SyntaxError:提交时语法无效

[英]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()

having trouble getting this to iterate over each row getting syntax errors and when it does run it only calculates the average of the first row and inputs that to all the rows of the database 在遍历每行中遇到语法错误时遇到麻烦,当它运行时,它仅计算第一行的平均值并将其输入数据库的所有行

what am i doing wrong to get it to iterate over each row and calculate the average and input it into the database? 我要怎么做才能遍历每一行并计算平均值并将其输入数据库?

pretty new to python so thanks in advance for your help. python非常新,因此在此先感谢您的帮助。

The problem isn't in Python, it's with your SQL syntax. 问题不在于Python,而是SQL语法。 The WHERE clause comes after SET : WHERE子句位于SET 之后

cur.execute("UPDATE combinations6 SET average = ? WHERE id = ?", (fun(row[0],row[1],row[2],row[3],row[4],row[5]), row)

Don't forget to swap the order of the substitution parameters to match this. 不要忘记交换替换参数的顺序来匹配它。

Also, you're using row as the parameter for id = ? 另外,您使用row作为id = ?的参数id = ? , which isn't right. ,这是不对的。 You can't put a whole list into a parameter, it has to be a specific element of the list, eg something like row[6] . 您不能将整个列表放入参数中,它必须是列表的特定元素,例如row[6]类的东西。 I don't know the actual position of the ID column in your table, so I don't know what the correct index is. 我不知道表中ID列的实际位置,所以我不知道正确的索引是什么。

You can also do the entire thing with a single query: 您还可以通过单个查询来完成整个操作:

UPDATE combinations6
SET average = (col1 + col2 + col3 + col4 + col5 + col6)/5

Replace col1 , etc. with the actual names of the columns you're computing the average of. 用要计算平均值的列的实际名称替换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()

strangely fixed with adding to the query and using a different pointer and the different query 奇怪地修复了添加到查询并使用不同的指针和不同的查询

num = numbers.fetchall()

Thanks for your help getting me there :) 感谢您的协助,让我到达这里:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM