-3

I am trying to insert a row into my table called users via mySQL. The following code outputs an error saying pymysql.err.InternalError: (1054, "Unknown column 'Mike' in 'field list'"). Or if I use the %s, %s, %s, then: NameError: name 'Mike' is not defined.

All I want to do is add a row with a user, what seems to be the problem?

import pymysql

# connect and login to db

conn = pymysql.connect(host = 'hosting',
                   user= 'user',
                   password= 'databasepassword',
                   db = 'databasename')

# create cursor object

a = conn.cursor()


# sql command


sql = 'INSERT INTO users(id, user, pwd) VALUES (2, Mike, password2)'

# execute sql commend

a.execute(sql)

1 Answers1

0

It thinks Mike is a column name because you are missing the quotes. Try:

import pymysql

# connect and login to db

conn = pymysql.connect(host = 'hosting',
                   user= 'user',
                   password= 'databasepassword',
                   db = 'databasename')

# create cursor object

a = conn.cursor()


# sql command


sql = "INSERT INTO users(id, user, pwd) VALUES (2, 'Mike', 'password2')"

# execute sql commend

a.execute(sql)
kjmerf
  • 4,275
  • 3
  • 21
  • 29