I have a list of values parsed from csv. the list includef information stored like in dictionary,I need to upload values from the list to MySql, tried to write a function, but an error occures.Error text : ProgrammingError: (1064, "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 ''Nokia' VALUE 'Manufacturer'' at line 1")
data = ["Manufacturer", "Nokia"]
def upload(what, where):
import MySQLdb as db
con = db.connect(host='localhost', user='root', passwd='somepass', db='test')
#connecting to MySql
cursor = con.cursor()
insertvalue = "a"
if what in where:
x = where.index(what)
x += 1
insertitem = where[x]
else:
insertitem = "Not Available"
query = "INSERT INTO phone_db %s VALUE %s"# (where to insert),(What to insert)
args = (insertitem, what)
#cursor.execute(query,args)
con.commit()
Your SQL query don't specify the columns that you are trying to fill with information.
Since you are using the '%s' syntax, python thinks you are trying to insert a string and not a column name, and it adds "'".
If you really want to specify the column this way (which is not recommended to say the least), you should do:
query = "INSERT INTO phone_db ({column}) VALUES (%s)".format(column = insertitem)
args = (what,)
cursor.execute(query,args)
More about the MySQL insert syntax and Python can be found here: https://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-transaction.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.