在 python 中执行 sql 命令

execute sql command in python

当我 运行 在我的 raspberry-pi 中使用这段代码时,什么也没发生,代码 运行 很顺利,但没有结果

#!/usr/bin/python
import MySQLdb

db = MySQLdb.connect(host="localhost", # your host, usually localhost
                 user="root", # your username
                  passwd="raspberry", # your password
                  db="raspberry") # name of the data base


cur = db.cursor() 


cur.execute("UPDATE visitors SET nb_visits = nb_visits+1 WHERE id = 1")

您应该commit您的交易才能使更改生效:

cur.execute("UPDATE visitors SET nb_visits = nb_visits+1 WHERE id = 1")
db.commit()