python to mysql unknown column in try exception -
i want select or insert mysql using python 3.2 , mysql.connector..
import mysql.connector filename = "t1.15231.0337.mod35.hdf" try: cnx = mysql.connector.connect(user='root', password='', database='etl') cursor = cnx.cursor() cursor.execute('select * hdf_file nama_file = %s',filename) rows = cursor.fetchall () if rows == []: insert_hdf = cursor.execute('insert hdf_file values(%s,null,now(),null,null,now())',filename) cursor.execute(insert_hdf) cnx.commit() cursor.close() cnx.close()
except mysql.connector.error err: print("something went wrong: {}".format(err))
but said that: unknown column 'filename' in clause have tried put this:
cursor.execute('select * hdf_file nama_file = filename')
but got same error...
when using cursor.execute()
parameterised queries query arguments passed sequence (e.g. list, tuple) or dictionary if using named parameters. code passes string filename
.
your queries written this:
cursor.execute('select * hdf_file nama_file = %s', (filename,))
here tuple (filename,)
passed execute()
. insert query:
cursor.execute('insert hdf_file values (%s, null, now(), null, null, now())', (filename,))
execute()
return none
, there no use in storing result in insert_hdf
variable. makes no sense, , cause error, if attempt cursor.execute(insert_hdf)
.
Comments
Post a Comment