首页 > 解决方案 > 如何在我的 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))

我试图将字段转换为字符串,但无法解决问题。一定有一些我在这里想念但无法找到的东西。

标签: mysqlpython-3.6

解决方案


推荐阅读