I am trying to insert data from my CSV file into SQL Server, but I am getting this error, which makes no sense to me..... because I am creating the table... shouldn't I be able to specify the data type? Also, even if I remove the CREATE table part of the code, the data is still not being INSERTED into the an already existing table.
ProgrammingError: ('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 12 (""): The supplied value is not a valid instance of data type float. Check the source data for invalid values. An example of an invalid value is data of numeric type with scale greater than precision. (8023) (SQLExecDirectW)')
import pandas as pd
import pyodbc
# Import CSV
data = pd.read_csv (r'C:\Users\Empyz\Desktop\Options_Data_Combined.csv')
df = pd.DataFrame(data)
df2 = df.replace('', np.nan, inplace=True)
# Connect to SQL Server
conn = pyodbc.connect('Driver={SQL Server};'
'Server=localhost;'
'Database=Stocks;'
'Trusted_Connection=yes;')
print('Connected Successfully to SQL Server')
cursor = conn.cursor()
# Insert DataFrame to Table
for row in df.itertuples():
cursor.execute('''
INSERT INTO OPTIONS_TEST2 (contractSymbol, lastTradeDate, strike, lastPrice, bid, ask, change, percentChange, volume, openInterest, impliedVolatility, inTheMoney, contractSize, currency
)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)
''',
row.contractSymbol,
row.lastTradeDate,
row.strike,
row.lastPrice,
row.bid,
row.ask,
row.change,
row.percentChange,
row.volume,
row.openInterest,
row.impliedVolatility,
row.inTheMoney,
row.contractSize,
row.currency
)
conn.commit()