'"TypeError: tuple indices must be integers or slices" - I am trying to import my data into SQL Server

I am trying to insert my data that is being pulled from yfinance in csv format to SQL server. I am getting the error "TypeError: tuple indices must be integers or slices, not list" - How can I fix up my code? I've tried using sqlite3 but that never seems to work for me. This version worked for my other scripts in inserting data to my sql server, but for this particular combined csv, I am getting this error. How can I fix up my code?

import yfinance as yf
import os
import glob
import pandas as pd
import pyodbc
os.chdir(r"C:\Users\Empyz\Desktop")






list1=['2022-03-18', '2022-03-25', '2022-04-01', '2022-04-08', '2022-04-14', '2022-04-22', '2022-05-20', '2022-06-17', '2022-07-15', '2022-10-21', '2023-01-20', '2024-01-19']

gme = yf.Ticker("gme")

for date in list1:
    df = gme.option_chain(date)
    df_call = df[0]
    df_put = df[1]
    
    #outputs options data to csv based on dates and type
    df_call.to_csv(f'C:\Empyz\Deskop\\call_{date}.csv', index=False)
    df_put.to_csv(f'C:\Empyz\Deskop\\put_{date}.csv', index=False)        

extension = 'csv'
#all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
all_filenames = [i for i in glob.glob(f'*.{extension}')]

#combine all files in the list
combined_pd = pd.concat([pd.read_csv(f) for f in all_filenames ])
#export to csv
combined_pd.to_csv( "Options_Data_Combined.csv", index=False)
print(combined_pd)


"""
Step 2 Specify columns we want to import
"""

columns = ['contractSymbol','lastTradeDate' ,'strike' ,'lastPrice' ,'bid' ,'ask' ,'change' ,'percentChange' ,'volume' ,'openInterest' ,'impliedVolatility' ,'inTheMoney' ,'contractSize','currency']

df_data = df[columns]
records = df_data.values.tolist()
    

"""
Step 3 Create database connection instance and connect to SQL Server
"""
conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=localhost;'
                      'Database=Stocks;'
                      'Trusted_Connection=yes;')
print('Connected Successfully to SQL Server')

"""
Step 4 Truncate (remove all data) from your table before inserting new records from your .csv file
"""
cursor = conn.cursor()
cursor.execute("TRUNCATE TABLE [Options_Data_GME]")
cursor.commit();
cursor.close()
print('SuccessfullyTRUNCATED TABLE [Options_Data_GME]')
    
  

"""
Step 4 Create a cursor connection and insert records
"""
sql_insert = '''
    INSERT INTO [Options_Data_GME] 
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)
'''



try:
    cursor = conn.cursor()
    cursor.executemany(sql_insert, records)
    cursor.commit();    
#except Exception as e:
    cursor.rollback()
   # print(str(e[1]))
finally:
    print('Successfully inserted data into table [Options_Data_GME].')
    cursor.close()
    conn.close()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source