I am trying to validating the excel data if it is > 12 chr length then i need to insert in a table (sql) with python code
I have tried with this code and i am getting the below error
'The SQL contains 0 parameter markers, but 1 parameters were supplied', 'HY000')
Value in excel already with closed brackets like ('12ewrr334dgdgskngk')
and i ran the query in SSMS and it is working fine INSERT INTO #finalresultset1 ( VIN ) Values ('12ewrr334dgdgskngk')
import xlrd
import pyodbc
book = xlrd.open_workbook(r'excelpath')
sheet = book.sheet_by_name(r'Sheet')
cnxn = pyodbc.connect('database connection')
cursor = cnxn.cursor()
query = """ INSERT INTO #finalresultset1 ( VIN ) Values """
VINSheet = sheet.ncols
for row in range(0,sheet.nrows):
for col in range(0,VINSheet):
cell_VIN = sheet.cell(row,col)
if len(cell_VIN.value) >= 12:
cursor.execute(query, cell_VIN.value)
else:
print('VIN Length must be greater than 17')
Tried
cursor.execute(query, (cell_VIN.value, ))
This time i got the different errorpyodbc.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC SQL Server Driver][SQL Server] Invalid object name '#finalresultset1'. (208) (SQLExecDirectW); [42S02] [Microsoft][ODBC SQL Server Driver][SQL Server] Statement(s) could not be prepared. (8180)")and I verified the temp table it is exist in my DB
EDIT
cursor.execute(" INSERT INTO #finalresultset1 ( product ) Values (?) ", cell_VIN.value)
cell_VIN.valueare a tuple('12ewrr334dgdgskngk').cursor.execute(query, (cell_VIN.value, ))cursor.execute(..., like in this Answer