Python INSERT不会写入数据库

时间:2015-11-10 02:42:32

标签: python mysql

所有数据库名称和表名都是正确的。 python脚本运行没有错误,但值永远不会写入数据库(表保持为空)`

datetimenowDB = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
deviceDB = "ALARM"
statusDB = "Tripped" 
typeDB = "None"
try:
    db = MySQLdb.connect(
    host="localhost",
    user="root",
    passwd="toor",
    db="log_database")
except Exception as e:
sys.exit('Cant get into database.')

c = db.cursor()

c.execute("INSERT INTO control_log (date_time, device, status, type) VALUES (%s,%s,%s,%s)",
(datetimenowDB, deviceDB, statusDB, typeDB))

1 个答案:

答案 0 :(得分:3)

MySQLdb会自动为您启动一个事务。您需要在完成更改后致电db.commit()

See this tutorial