mysql - 如何在我的 Python 脚本中修复 MySQL 插入查询?
问题描述
我正在尝试使用 Python 3.6 在 mysql 中插入一些数据,但是在执行查询时遇到以下错误-
sql="Insert into log (merchant_id,products_count_before,products_count_after,feed_count_before,feed_count_after) values("+escape_str(merchant_id)+","+escape_str(products_count_before)+","+escape_str(products_count_after)+","+escape_str(feed_count_before)+","+feed_count_after+")"
TypeError: must be str, not int
我的表结构如下 -
CREATE TABLE log (
id INT(11) NOT NULL AUTO_INCREMENT,
merchant_id INT(11) NOT NULL,
products_count_before INT(11) NULL,
products_count_after INT(11) NULL,
feed_count_before INT(11) NULL,
feed_count_after INT(11) NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
我的python函数-
def upload_log_summery(self,merchant_id):
sql="Insert into etl_log
(merchant_id,products_count_before,products_count_after,
feed_count_before,feed_count_after)
values("+escape_str(merchant_id)+","+escape_str(products_count_before)
+","
+ escape_str(products_count_after)+","+escape_str(feed_count_before)
+","+feed_ count_after+")" self.connector.execute(sql,
[escape_str(merchant_id),escape_str(products_count_before),
escape_str(products_count_after),escape_str(feed_count_before),
escape_str(feed_count_after)])
def escape_str(string):
return escape_string(str(string))
我试图将字段转换为字符串,但无法解决问题。一定有一些我在这里想念但无法找到的东西。
解决方案
推荐阅读
- php - 获取以逗号分隔的结果
- c# - 为什么 animSetInteger 不能正常工作?
- oracle - 优化 Oracle 中的嵌套循环查询
- python - CONLL 2003 BIO 格式的 NER 数据准备
- c++ - 删除copy-ctor和copy-assignment - public、private还是protected?
- sorting - 8 位整数的基数排序(降序),得到排序索引。如何?
- vuetify.js - 滑块的拇指颜色变为灰色
- ssh - 组合 SSH 命令
- c# - 如何引用添加到 Visual Studio 项目中的 json 文件
- haskell - 阅读器和解析器有什么区别?