I have extracted filename from a certain directory. Filename is a string variable. Such as: 'LineNo-YYMMDD_hhmm'. Now I want to insert the filename into the existing MySQL DB. But couldn't find how to add as a str variable. This is what I had tried. Have some other way also checking net.
....
file_prefix = os.path.basename(name)[: - (len(filetail))]
(file_prefix is: LineName-200215_1619)
try:
connection = mysql.connector.connect(
host='localhost', db = 'db_name',
user='usr', password='myPass')
mysql_insert_query = " INSERT INTO fileHeader (headInfo) VALUES %s" % str(file_prefix)
cursor = connection.cursor()
cursor.execute(mysql_insert_query)
connection.commit()
cursor.close()
except mysql.connector.Error as error:
print ("Failed to insert record into fileHeader table {}".format(error))
Getting the following error:
Failed to insert record into fileHeader table 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
'LineName-200215_1619' at line 1