Python/MySql良好的游标.执行但不是游标.executemany. 'TypeError:并非所有参数都在字符串格式化过程中转换'

2024-07-04 10:46:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个函数,做一个mysql输入一个csv文件。它是逐行导入的,但我想加快导入速度,并使用执行官. 在我使用了6个月的代码的第一个版本中,它会在字段拆分后逐行循环文档,每行调用一次mysql insert。今天我修改了函数,在列表列表上使用cursor.executemany而不是cursor.execute构建了一个名为multi_line_rebuild的列表列表。我把剩下的代码都留下了。作为调试过程的一部分,我确实在cursor.executemany之前打印了multi_line_rebuild,当我调试它时,它看起来像一个很好的列表列表。另外,当我切换回代码时,它也可以正常工作。但是,当我尝试用cursor.executemany进行较大的插入时,它抛出了一个错误“TypeError:在字符串格式化期间并非所有参数都已转换”。不确定是否要在调试过程中执行下一步操作

全套错误是

  • 文件“C:\Python35\lib\site packages\pymysql\光标.py,第193行,在executemany中 self.\u get \u db().encoding)
  • 文件“C:\Python35\lib\site packages\pymysql\光标.py,第209行,在\u do \u execute \u many中 v=值%escape(next(args),conn)
  • TypeError:不是所有参数都在字符串格式化期间转换

“”“

def run_query_with_warnings(warn_type, query_string, **kargs):
   cursor = db.cursor()
   cursor.executemany(query_string, kargs['field_split'])


....main function...
query_string = 'INSERT into ' + table_name + ' (' + report_field_list_reformat_mysql + ') VALUES (%s);' % var_string
run_query_with_warnings(1, query_string, field_split=multi_line_rebuild)

 query_string
'INSERT into tbl_rpt_GET_MERCHANT_LISTINGS_DATA_ (`item-name`, '
'`item-description`, `listing-id`, `seller-sku`, `price`, `quantity`, '
'`open-date`, `image-url`, `item-is-marketplace`, `product-id-type`, '
'`zshop-shipping-fee`, `item-note`, `item-condition`, `zshop-category1`, '
'`zshop-browse-path`, `zshop-storefront-feature`, `asin1`, `asin2`, `asin3`, 
''`will-ship-internationally`, `expedited-shipping`, `zshop-boldface`, '
'`product-id`, `bid-for-featured-placement`, `add-delete`, '
'`pending-quantity`, `fulfillment-channel`, `merchant-shipping-group`, '
'`update_time`, `user`, `report_id`) VALUES (%s);'

kargs['field_split']
[[b'Bronze',
  b'',
  b'09234VKV44OX',
  b'KL-RZNQ',
  b'34.9',
  b'',
  b'2019-09-09 14:04:42 PDT',
  b'',
  b'y',
  b'1',
  b'',
  b'',
  b'11',
  b'',
  b'',
  b'',
  b'B0231QB57A',
  b'',
  b'',
  b'',
  b'',
  b'',
  b'B023412315QA',
  b'',
  b'',
  b'',
  b'NA',
  b'Migrated Template',
  '2019-09-21 22:24:17',
 'adam',
 '16728972805018160'],
[b'3 Patches',
  b'',
  b'07223O5PFP5',
  b'Q2-442RI-MA38',
  b'23.73',
  b'',
  b'2019-07-23 19:26:14 PDT',
  b'',
  b'y',
  b'1',
  b'',
  b'',
  b'11',
  b'',
  b'',
  b'',
  b'B02342P7TTW',
  b'',
  b'',
  b'',
  b'',
  b'',
  b'B02342MP7TTW',
  b'',
  b'',
  b'',
  b'NA',
  b'Migrated Template',
  '2019-09-21 22:24:17',
  'adam',
  '16728972805018160']]

Tags: 文件代码idfield列表stringlinemysql

热门问题