oct*_*bus 16 python mysql-python
我收到很多同样的警告 -
Warning: Truncated incorrect DOUBLE value: '512121500B'
Run Code Online (Sandbox Code Playgroud)
- 但无法弄清楚原因.
MySQL表dr_snapshot看起来像这样:
PremiseID char(10) Primary Key
cycle SMALLINT(6)
last_read DATETIME
reading INTEGER
DeviceID INTEGER
Run Code Online (Sandbox Code Playgroud)
我想通过PremiseID删除很多行.这是一个特别的:'512121500B'.
这是我的功能,由PremiseID删除:
def delDrSnapRow(premiseID, db):
sql_cmd = " ".join([
"delete ",
"from dr_snapshot ",
"where PremiseID = " + str(premiseID) + " ; "])
del_cur = db.cursor()
rc = del_cur.execute(sql_cmd)
del_cur.close()
return rc
Run Code Online (Sandbox Code Playgroud)
PremiseID是一个字符串,而不是double.我在这里错过了什么?
非常感谢.
修改我的删除程序后使用try:..除了,我没有看到警告.我相信我没有看到警告,因为我没有看到警告显示 - print(e)- 在除了部分.
我得到的结论是尝试:除了以某种方式删除了错误.
def delDrSnapRow(premiseID, db):
sql_cmd = " ".join([
"delete ",
"from dr_snapshot ",
"where PremiseID = " + "'" + premiseID + "' ; "])
del_cur = db.cursor()
try:
rc = del_cur.execute(sql_cmd)
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
except MySQLdb.Warning, e:
print(e)
rc = del_cur.execute(sql_cmd)
del_cur.close()
return rc
Run Code Online (Sandbox Code Playgroud)
Man*_*war 17
看看这一行"where PremiseID = " + str(premiseID) + " ; "])".比较发生在不同的类型上,当MySQL比较不同的数据类型时,它们会在比较之前内部转换为DOUBLE.因此,您可以尝试单独引用或围绕解决问题.所以这不是尝试捕获,而是解决问题的报价.
不要滚动自己的查询字符串构建器,它会导致各种问题.使用Python SQL库的内置查询构造语法.如果您至少使用MySQLdb,您应该可以执行以下操作:
rc = del_cur.execute('delete from dr_snapshot where PremiseID = %s', (premiseID,))
Run Code Online (Sandbox Code Playgroud)