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