是否有更快的方式来插入这些字典记录?字典" final"一般有大约153,000条记录。所有记录的插入时间目前在核心i5处理器上为1分18秒,在本地执行所有操作。
def output_db(final, sqlite_file):
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
field_names_dict = get_field_names(final)
sql_field_names = ",".join(field_names_dict.keys())
for key, row in final.items():
insert_sql = 'INSERT INTO asset(' + sql_field_names + ') VALUES ("' + '","'.join(field_value.replace('"', "'") for field_value in list(row.values())) + '")'
c.execute(insert_sql)
conn.commit()
conn.close()
以下是每行数据通常的样子:
("computer_name", comp_name),
("domain_name", domain),
("serial_num", serial),
("device_type", type),
("mfr", mfr),
("model", model),
("os_type", os_type),
("os_ver", os_ver),
("os_subver", os_subver),
("country", country),
("state", state),
("history", "\n".join(hist)),
("cmdb_resource_name", cmdb_resource_name),
("cmdb_dns_name", cmdb_dns_name),
("cmdb_serial_number", cmdb_serial),
("cmdb_family", cmdb_family),
("cmdb_manufacturer", cmdb_manufacturer),
("cmdb_model", cmdb_model),
("cmdb_class", cmdb_class),
("cmdb_operating_system", cmdb_operating_system),
("cmdb_location_name", cmdb_location_name),
("cmdb_country", cmdb_country),
("bay_name", bay_name),
("bay_dns_name", bay_dns_name),
("bay_mfr", bay_mfr),
("bay_model", bay_model),
("bay_serial_number", bay_serial_number),
("bay_device_type", bay_device_type),
("bay_os_name", bay_os_name),
("bay_os_sp", bay_os_sp),
("bay_os_ver", bay_os_ver),
("tan_id", tan_id),
("tan_comp_name", tan_comp_name),
("tan_os", tan_os),
("tan_os_build", tan_os_build),
("tan_os_sp", tan_os_sp),
("tan_country_code", tan_country_code),
("tan_mfr", tan_mfr),
("tan_model", tan_model),
("tan_serial", tan_serial)