Whenever I run my Python code, I want it to truncate the table in MYSQL and then insert all the rows. I tried doing a TRUNCATE statement, followed by an INSERT INTO statement, but once the code is finished executing, I only see the last record in there. This is what my code looks like.
import pandas as pd
from sodapy import Socrata
import requests
import json
import pymysql
client = Socrata("data.cdc.gov", None)
results = client.get("9mfq-cb36", limit=100)
con = pymysql.connect(host = 'x.x.x.x', user = 'x', passwd = 'xxx', db = 'xxx')
cursor = con.cursor()
# for every column, get the value of the cell as it goes through each row
for val in results:
try:
submission_date = val['submission_date']
except(KeyError):
submission_date = None
try:
state = val['state']
except(KeyError):
state = None
# continues getting vals for each column with try statements
cursor.execute("TRUNCATE TABLE covid19_cases_t")
cursor.execute("INSERT INTO covid19_cases_t (`submission_date`, `state`, `tot_cases`, `conf_cases`, `prob_cases`, `new_case`, `pnew_case`, `tot_death`, `conf_death`, `prob_death`, `new_death`, `pnew_death`, `created_at`, `consent_cases`, `consent_deaths`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", (submission_date, state, tot_cases, conf_cases, prob_cases, new_case, pnew_case, tot_death, conf_death, prob_death, new_death, pnew_death, created_at, consent_cases, consent_deaths))
con.commit()
con.close()
I would like it so that once I execute the code, it truncates any data that's already in the table once, then inserts all rows again. The insert into statement works just fine, and if I don't have the truncate statement there, it executes all the rows. If I do have the truncate statement there, it truncates every record that the Insert Into statement inserts except for the very last record. I saw this StackOverflow post, but it doesn't deal with Python. The answer recommends to insert all statements in one go rather than one by one, but I don't know how to do that within Python. For anyone who has conceptual suggestions, please assist with coding that.
Am I supposed to make my insert statement different? Move stuff around in the code? Create a loop? Please help and thanks.