2017-08-09 67 views
0

信息:插入统计信息,即温度值。该信息应该是“临时”,让我知道,日志是关于温度Python SQL查询 - 未知列

我试图从一个Python脚本

info = "temp" 
sql = 'insert into stat_bus (addr, grp, info, status) 
VALUES (' + address + ', ' + group + ', ' + info + ', ' + status + ')' 

我获得以下错误执行一个查询:未知柱温度在字段列表 地址,组和状态不会导致错误。

我尝试过其他的可能性为字符串解析(我想这是错误?),但不能得到它的工作。在此先感谢

+0

你对数据库连接有什么用?所有实现[Python数据库API](https://www.python.org/dev/peps/pep-0249/)的库都应该为您进行转义。您不应该自行构建查询。 – Kendas

+0

考虑使用准备语句http://zetcode.com/db/mysqlpython/ – damians

回答

1

的问题是,你的查询,SQL不解释temp作为该字符串“TEMP”,但作为一个变量命名temp

如果打印sql,你会得到下面的查询:

insert into stat_bus (addr, grp, info, status) VALUES (something, something, temp, something) 

而你想这个查询:

insert into stat_bus (addr, grp, info, status) VALUES ("something", "something", "temp", "something") 

(注意周围temp引号)

只是改变你的Python代码在正确的位置加上引号:

info = "temp" 
sql = 'insert into stat_bus (addr, grp, info, status) 
VALUES ("' + address + '", "' + group + '", "' + info + '", "' + status + '")' 
+0

谢谢,这解决了我的问题。 –