SyntaxError:commi上的语法无效

2024-09-29 21:31:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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还不熟悉,所以提前感谢您的帮助。在


Tags: importnumpy数据库executeconnectconnsqlite3list
2条回答

问题不在Python中,而是与SQL语法有关。WHERE子句位于^{之后:

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

别忘了交换替换参数的顺序来匹配这个。在

另外,您使用row作为id = ?的参数,这是不对的。你不能把一个完整的列表放入一个参数中,它必须是列表的一个特定元素,例如row[6]。我不知道ID列在表中的实际位置,所以我不知道正确的索引是什么。在

您也可以使用单个查询完成整个操作:

^{pr2}$

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

奇怪地修复了添加到查询并使用不同的指针和不同的查询

^{pr2}$

谢谢你的帮助,把我带到那里:)

相关问题 更多 >

    热门问题