Python MySQL-选择有效,但不能删除? [英] Python MySQL - SELECTs work but not DELETEs?

查看:183
本文介绍了Python MySQL-选择有效,但不能删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python和Python的MySQL适配器的新手.我不确定在这里是否缺少明显的东西:

I'm new to Python and Python's MySQL adapter. I'm not sure if I'm missing something obvious here:

db = MySQLdb.connect(# db details omitted)
cursor = self.db.cursor()

# WORKS
cursor.execute("SELECT site_id FROM users WHERE username=%s", (username))
record = cursor.fetchone()

# DOES NOT SEEM TO WORK
cursor.execute("DELETE FROM users WHERE username=%s", (username))

有什么想法吗?

推荐答案

我猜您正在使用支持事务的存储引擎(例如InnoDB),但是在DELETE之后您不会调用db.commit().如果您不提交,则DELETE的效果将被丢弃.

I'd guess that you are using a storage engine that supports transactions (e.g. InnoDB) but you don't call db.commit() after the DELETE. The effect of the DELETE is discarded if you don't commit.

请参见 http: //mysql-python.sourceforge.net/FAQ.html#my-data-disappeared-or-won-t-go-away :

从1.2.0开始,MySQLdb禁用 默认情况下,自动提交 DB-API标准(PEP-249).如果你 正在使用InnoDB表或其他一些表 事务表类型的类型, 您需要执行connection.commit() 在关闭连接之前,否则 您所做的任何更改都不会被写入 到数据库.

Starting with 1.2.0, MySQLdb disables autocommit by default, as required by the DB-API standard (PEP-249). If you are using InnoDB tables or some other type of transactional table type, you'll need to do connection.commit() before closing the connection, or else none of your changes will be written to the database.

另请参见类似的SO问题: Python MySQLdb更新查询失败

See also this similar SO question: Python MySQLdb update query fails

这篇关于Python MySQL-选择有效,但不能删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆