Search code examples
python-3.xsql-insertpymysql

pymysql operational error 1054: inserted values are read as columns


I've been recently struggling with an error when I am trying to use the following code to insert a line of code into a table using pymysql:

sql = 'INSERT INTO `db`.`table`(`cola`,`colb`) VALUES (`%s`,%s);'
vals = ('This is a string',227.77)
cur.execute(sql,vals)

I also commit it, but I don't think that forgetting to commit is the issue here. When I run my code, I end up getting the error:

pymysql.err.OperationalError: (1054, "Unknown column ''This is a string'' in 'field list'")

I am not sure if I did something wrong here, because when I use pymysql to look at the columns in db.table, both cola and colb are present. Also, I do not know why something in VALUES() is ending up being read as a column? The same thing happens when I try other methods of formatting the string.

Thank you so much for any insights you may have! I really appreciate it!


Solution

  • I figured it out. I added double quotes around the %s and it worked okay for strings. Inserting a number works fine in the way I wrote above.