如何在我的Python脚本中修复MySQL插入查询?

时间:2018-08-02 04:50:44

标签: mysql python-3.6

我正在尝试使用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))

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

0 个答案:

没有答案