I am trying to load the data from the REST API into SQL server table using the Python script. The script below works perfectly but the issues is it takes too long to upload the data in to the database , it took few minutes to load 8000 records
import requests
import pandas as pd
import pyodbc
import sys
from requests.auth import HTTPBasicAuth
#SQL Connection
conn_str = (
r'DRIVER={SQL Server Native Client 11.0};'
r'SERVER=tcp:abcd.database.windows.net;'
r'DATABASE=DEF;'
r'UID=xxxxx;'
r'PWD=yyyy;'
r'Trusted_Connection=no;'
)
sqlconn = pyodbc.connect(conn_str)
cursor = sqlconn.cursor()
cursor.execute("TRUNCATE TABLE [dbo].[RM_Approved_Room_State]")
sqlconn.commit()
#API Connection
baseURL = 'https://testing.org/api/fdf'
appRoomsResponse = requests.get(baseURL, verify=False)
appRoomsJson = appRoomsResponse.json()
appRoomsDF = pd.json_normalize(appRoomsJson)
appRoomsDF = appRoomsDF.fillna('')
try:
cursor = sqlconn.cursor()
for index,row in appRoomsDF.iterrows():
cursor.execute("INSERT INTO RM_Approved_Room_State(APPROVED_ROOM_STATEID,SOURCE_ROOMID,DEST_ROOMID,ENTITY_TYPEID)\
values(?,?,?,?)"
,row['id']
,row['sourceRoomRefId']
,row['destinationRoomRefId']
,row['entityRefId']
)
sqlconn.commit()
except Exception:
pass
#Load main Dim_appRooms table
cursor = sqlconn.cursor()
sqlconn.commit()
cursor.close()
sqlconn.close()
Is there something I am missing to increase the speed of the insert here. My first script in Python, anyhelp is greatly appreciated