【发布时间】:2017-10-23 07:42:08
【问题描述】:
从我的调试日志中,我发现我的进程被中断了,我在我的代码中设置了try-catch,如下所示,
try:
print "update query : ........"
query = ("Update Logs set id = "+str(id)+" where Sid = '"+str(id)+"'")
cursor.execute(query)
conn.commit()
except (MySQLdb.Error, MySQLdb.Warning) as e:
sqlerrlogs = open("/log/mysqlerr.log","a")
sqlerrlogs.write(str(datetime.now()))
sqlerrlogs.write('\n')
sqlerrlogs.write(e)
sqlerrlogs.write('\n\n')
但是从我的日志中,在打印“更新查询:........”之后,我的日志中没有错误,如何修复我的代码以了解发生了什么? 我的python版本是2.7
【问题讨论】:
-
你是如何运行你的进程的?
-
每两分钟运行一次 cron
-
你真的不想在你的数据上使用字符串插值,你很容易受到 SQL 注入攻击。使用查询参数(而不是
" + str(value) + "使用占位符%s并将一系列值作为第二个参数传递给cursor.execute())。 -
您是否考虑过
where Sid = ?查询根本不匹配任何行的可能性?你怎么知道有什么东西被打断了? -
Sid 值是存在的,因为从调试日志中,在执行更新查询之前,我有 print "update query : Update Logs set id = "+str(id)+" where Sid = '" +str(id)+"'"