下表是我的MySQL字段数据
名为infor
的表
+-------------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+-------------------+----------------+
| id | bigint(7) | NO | PRI | NULL | auto_increment |
| temperature | varchar(20) | YES | | NULL | |
| wet | varchar(20) | YES | | NULL | |
| created | timestamp | NO | | CURRENT_TIMESTAMP | |
+-------------+-------------+------+-----+-------------------+----------------+
当我想将数据上传到我的 MySQL 服务器时
我使用以下代码:
temperature_item = '8.7`c'
wet_item = '0.87%'
cur.execute("INSERT INTO infor (temperature,wet) VALUES (temperature_item,wet_item)")
然后它显示以下错误:
pymysql.err.InternalError: (1054, "Unknown column 'wet_item' in 'field list'")
但是当我输入:
cur.execute("INSERT INTO infor (temperature,wet) VALUES ('87`c','8.7%')")
它可以工作,我已经搜索了上一个问题
我添加了不可打印的词吗?还是其他问题?
最佳答案
"INSERT INTO infor (temperature,wet) VALUES (temperature_item,wet_item)"
这是一条Python string literal 。您的 cur.execute
语句与在 mysql 控制台中输入以下内容完全相同。
INSERT INTO infor (temperature,wet) VALUES (temperature_item,wet_item)
现在你在mysql中定义了wet_item吗?不,所以结果是上面的错误。
这也是一个字符串文字
"INSERT INTO infor (temperature,wet) VALUES ('87c','8.7%')"
但是这里你将常量传递给mysql,它可以很容易地处理这些常量。如果您想将变量传递给 mysql,正确的形式是
cur.execute("INSERT INTO infor (temperature,wet) VALUES(%s, %s)", (temperature_item,wet_item))
关于python - 从Python到MySQL上传数据失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40971866/