alwbtc
alwbtc

Reputation: 29495

Error when inserting row to table with MySQLdb

What is wrong below?

import MySQLdb as mysql
import datetime

db = mysql.connect("localhost","root","passworld","employees" )
cursor = db.cursor()

sql = "INSERT INTO employee(id, firstname, surname, sex, employmentdate) VALUES (%s, %s, %s, %s, '%s')" %(id, firstname, surname, sex, employmentdate)

dater = datetime.datetime(2005,1,1)
cursor.execute(["012345","Mark", "Rooney", "M", dater])

OperationalError: (1054, "Unknown column 'Mark' in 'field list'")

Upvotes: 1

Views: 83

Answers (1)

alecxe
alecxe

Reputation: 474181

You should pass your sql statement and params to cursor.execute():

sql = "INSERT INTO employee(id, firstname, surname, sex, employmentdate) VALUES (%s, %s, %s, %s, '%s')"
cursor.execute(sql, ["012345","Mark", "Rooney", "M", dater])
db.commit()

Upvotes: 1

Related Questions