在python中通过MySQL进行迭代的问题

问题描述 投票:0回答:1

我刚刚开始学习python,正在尝试项目。我的python代码有一个。我要做的是依次遍历表中的行,并对每一行执行特定的任务。以一个示例为例,我有多列的行,其中三列是con_access,examinationtotal。现在,我想获取con_accessexam]中的值之和>列,然后将其放在总计列中。此计算应一个接一个地进行。问题在于程序转到最后一行,取总变量,并互相填充。

下面是我的代码

def result_total():
    mdb = mysql.connector.connect(
        host="localhost",
        user="root",
        passwd="**************",
        database="majesty"
    )

    mycursor = mdb.cursor()
    # mycursor.execute("SELECT con_access, exam FROM students")
    mycursor.execute("SELECT CAST(con_access AS UNSIGNED) as con_access, CAST(exam AS UNSIGNED) as exam FROM students")
    rows = mycursor.fetchall()
    for row in rows:
        if row:
            total = row['con_access'] + row['exam']
            sql = "UPDATE students SET total = {}".format(total)
            mycursor.execute(sql)
            mdb.commit()

我刚刚开始学习python,正在尝试项目。我的python代码有一个。我要做的是依次遍历表中的行,然后在...

python mysql
1个回答
0
投票

找到总数如下

© www.soinside.com 2019 - 2024. All rights reserved.