python - 使用 MySQLdb 向表中插入行时出错

标签 python mysql sql mysql-python

下面有什么问题吗?

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'")

最佳答案

您应该将您的 sql 语句和参数传递给 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()

关于python - 使用 MySQLdb 向表中插入行时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18638499/

相关文章:

php - 列连接返回 "Null"Mysql - Php

sql - 复杂的 MySQL Order by 不工作

Python 错误 - 类型错误 : bad operand type for unary -: 'NoneType'

python - 从python中的请求查询结果中提取一个json字段

python - 在 Python 应用程序中安排日常任务

python - 在 virtualenvwrapper 中激活环境

Mysql IF 语句

php - MYSQL 查询连接问题

sql - 带有 OR 运算符的列的复合索引

sql - 附加 MDF 文件而不使用 LDF 文件