I am trying to insert submissions from ODK central API into MySQL table. Only 10 records out of 23,751 are inserted. I've also tried inserting from a JSON file I created with the downloaded data. See the code below:
import json
import mysql.connector
from datetime import datetime
db = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="mapped_customers"
)
f = open("submissions.json", "r")
data=f.read()
data= json.loads(data)
count=0
try:
for customer in data['value']:
count+=1
print(count)
# Input timestamp in ISO 8601 format
timestamp = customer['__system']['submissionDate']
# Convert to datetime object
dt_object = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ')
# Format datetime object as MySQL datetime string
mysql_datetime = dt_object.strftime("%Y-%m-%d %H:%M:%S")
cust_name=customer['shop_name']
cust_contact=customer['daily_contact_number']
contact_person=customer['shop_owner']
cust_category=customer['customer_category']
latitude=customer['store_gps']['coordinates'][1]
longitude=customer['store_gps']['coordinates'][0]
cust_img=customer['photo']
location=customer['zone_name']
landmark=customer['landmark']
mapper=customer['mapper']
submission_date=mysql_datetime
importance=customer['importance']
sales=customer['sales']
purchases=customer['purchases']
alt_contact=customer['alternative_phone_number']
cursor = db.cursor()
sql="INSERT INTO gsmrt_cstmr_vrfctn(CSTMR_NME,CST_CNTCT,CNTCT_PRSN,CSTMR_CLSS,CSTMR_LTTD,CSTMR_LNGTD,CSTMR_IMG,CSTMR_LCTN,CSTMR_LND_MRK,MAPPER,SUBMSN_DTE,IMPORTNCE,SLS,PURCHSES,ALT_CNTCT) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
val=(cust_name,cust_contact,contact_person,cust_category,latitude,longitude,cust_img,location,landmark,mapper,submission_date,importance,sales,purchases,alt_contact)
cursor.execute(sql,val)
db.commit()
db.close()
except (mysql.connector.Error, mysql.connector.Warning) as e:
print(e)
print("Done!")
What could I be doing wrong?
NB: The loop is able to successfully traverse through all the records and no timeout error is thrown.