Mysql的,蟒蛇,SQL不执行
问题描述:
您好我有这个基本的MySQL代码:Mysql的,蟒蛇,SQL不执行
sql = 'INSERT INTO popularity (PersonNumber, Category, Value) VALUES (1,"Tennis","great")'
connection = MySQLdb.connect(host = "localhost", user = "***",
passwd = "***", db = "inb104")
cursor = connection.cursor()
cursor.execute(sql)
cursor.close()
connection.close()
然而,这并没有任何插入到数据库。为什么? sql语法是正确的,因为我已经将输出的sql变量直接复制到mysql控制台中。我知道这段代码正在工作,因为如果我这样做:sql =' SELECT * FROM popularity'
我可以在python中输出结果。
答
connection.commit()
,会做的伎俩= d
答
因为您在关闭游标之前忘记了提交事务。
答
您需要提交事务。