python: convert result from pymysql fetchall query to an importable insert to another database.

sql_prepare = """            XXXXXXXXXXXXXXXXXX  """

cur_mg_123091123.execute(sql_prepare)
result = cur_mg_123091123.fetchall()
result_list_leads = [list(i) for i in result]

 

 

# convert result to insert format
list_to_string = ''.join(str(result_list_leads))
list_to_string_characters = list_to_string.replace('[','(').replace(']',')').replace('None','NULL').replace('((','(').replace('))',')')
 
 
 
 
sql_insert = """

INSERT INTO crm.a_temp_mautic_mg_stats (lead_id, crm_email_id, email_address, points, lead_mkt, lead_mkt_text, email_count, email_last_date_sent,
email_last_opened) VALUES %s;

"""
print(sql_insert % (list_to_string_characters))
cur_crm_42098123.execute(sql_insert % (list_to_string_characters))
conn_crm_42098123.commit()

 
 

Leave a Reply

Your email address will not be published. Required fields are marked *