can you please explain why that code doesn't work
Code (Python):
date = row['actionTime'] #date
my_time = date.strftime('%Y-%m-%d %H:%M:%S')
t_dict[ts] = my_time
placeholders = ', '.join(['%s'] * len(t_dict))
columns = ', '.join(t_dict.keys())
sql = "INSERT INTO {0} ( {1} ) VALUES ( {2} );".format(table, columns, placeholders)
sql_insert = sql %(tuple(t_dict.values()))
cursor = mydb.cursor()
cursor.execute(sql_insert,)
cursor.close()
mydb.close()
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 '12:23:34 )' at line 1
Insert value:
INSERT INTO visits ( personId, visitDt ) VALUES ( 12345, 2010-01-01 12:23:34 );
<class 'str'> - class of 2010-01-01 12:23:34. class 'date' give a same error
Code (Python):
date = row['actionTime'] #date
my_time = date.strftime('%Y-%m-%d %H:%M:%S')
t_dict[ts] = my_time
placeholders = ', '.join(['%s'] * len(t_dict))
columns = ', '.join(t_dict.keys())
sql = "INSERT INTO {0} ( {1} ) VALUES ( {2} );".format(table, columns, placeholders)
sql_insert = sql %(tuple(t_dict.values()))
cursor = mydb.cursor()
cursor.execute(sql_insert,)
cursor.close()
mydb.close()
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 '12:23:34 )' at line 1
Insert value:
INSERT INTO visits ( personId, visitDt ) VALUES ( 12345, 2010-01-01 12:23:34 );
<class 'str'> - class of 2010-01-01 12:23:34. class 'date' give a same error