Trying to add values from a worksheet to a MySQL Table using a Python loop.
The code is as follows:
for i in range(number_of_columns):
column_title=worksheet.cell(0,i).value
for n in range(1,number_of_rows+1):
value_for_table=worksheet.cell(n,i).value
print n,i,column_title, value_for_table
mycursor.execute("INSERT INTO Test (%s) VALUES (%s)", (column_title,value_for_table))
It spits out the following error:
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Name') VALUES ('Germany')' at line 1
I tried the solutions here unsuccessfully
Edit:
The Columns were originally created with this code:
for i in range(number_of_columns):
column_title=worksheet.cell(0,i).value
datatype_of_column=type(worksheet.cell(1,i).value)
if datatype_of_column==float:
cell_type="FLOAT(10)"
elif datatype_of_column==int:
cell_type="INT(10)"
elif datatype_of_column==unicode or datatype_of_column==str:
cell_type="VARCHAR(10)"
elif datatype_of_column==bool:
cell_type="BINARY"
else:
print ('Datatype at column %s not recognized. Set to float.', i)
mycursor.execute("ALTER TABLE Test ADD (%s %s)" % (column_title, cell_type))