如何修复python脚本中的mysql insert查询?

pnwntuvh  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(214)

我试图使用python3.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))

我试图将字段转换为字符串,但无法解决问题。一定有什么东西我不知道,但却找不到。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题