簡體   English   中英

如何使用 Python 將列表中的多維數據插入到 MYSQL

[英]How to insert multi dimensional data from a list to MYSQL using Python

我想將offer_titleOffer_details的值輸入到表comp_offer_records中。 有人可以幫忙嗎。

quotes =[{'Offer_Title': 'New credit ', 'Offer Details': 'The Centre'},
         {'Offer_Title': 'New credit1 ', 'Offer Details': 'The Centre1'}]
#If I flatten the list as below:
flat_list = [item for sublist in quotes for item in sublist]

How do I then ensure values corressponding to Offer_Title and Offer Details get loaded into separate columns?
params = ['?' for item in flat_list]
sql    = 'INSERT INTO comp_offer_records (Offer_Title, Offer_Details) VALUES (%s, %s);' % ','.join(params)
mycursor.executemany(sql, flat_list)

但是,這會產生錯誤:

sql = '插入 comp_offer_records (Offer_Title, Offer_Details) 值 (%s, %s);' % ','.join(參數)

類型錯誤:沒有足夠的 arguments 用於格式字符串

'(%s, %s);' % ','.join(params)

預期 2 字符串 arguments,當您通過 1

將其替換為

'(%s, %s);' % params

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM