我用MySQLdb创建了一个数据库。
在数据库中,我有名字student
一个表列:设置%s的Mysqldb更新错误
id(is int), id_user(is int), f_name(is str), l_name(is str)
我想更新一行。
我的代码如下:
db=mdb.connect(host="localhost", use_unicode="True", charset="utf8",
user="", passwd="", db="test")
# prepare a cursor object using cursor() method
cursor = db.cursor()
sql="""SELECT id_user FROM student"""
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()
rows = cursor.fetchall()
the=int(7)
se=str('ok')
for row in rows:
r=int(row[0])
if r==the:
sql2 = """UPDATE student
SET f_name=%s
WHERE id_user = %s"""% (se,the)
# Execute the SQL command
cursor.execute(sql2)
# Commit your changes in the database
db.commit()
db.rollback()
# disconnect from server
db.close()
当我运行它,我采取的错误是列名称为好,为什么?
任何人都可以帮我找到我做错了吗?
你应该附上字符串转换为单引号。 – 2012-03-16 18:53:29