Sql insert bulk list of dictionaries

This article is tagged with: Sql insert bulk list of dictionaries and mysql

sql insert bulk list of dictionaries

for item_code, companywise_item_data in items_default_data.items():
for company, item_default_data in companywise_item_data.items():
to_insert_data.append((
frappe.generate_hash("", 10),
item_code,
'Item',
'item_defaults',
company,
item_default_data.get('default_warehouse'),
item_default_data.get('expense_account'),
item_default_data.get('income_account'),
item_default_data.get('buying_cost_center'),
item_default_data.get('selling_cost_center'),
))
if to_insert_data:
frappe.db.sql('''
INSERT INTO `tabItem Default`
(
`name`, `parent`, `parenttype`, `parentfield`, `company`, `default_warehouse`,
`expense_account`, `income_account`, `buying_cost_center`, `selling_cost_center`
)
VALUES {}
'''.format(', '.join(['%s'] * len(to_insert_data))), tuple(to_insert_data))

This article is tagged with: Sql insert bulk list of dictionaries and mysql