10

我是 Python 和 Python 的 MySQL 适配器的新手。我不确定我是否在这里遗漏了一些明显的东西:

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

有任何想法吗?

4

4 回答 4

12

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.

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

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.

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

于 2009-09-20T20:14:52.610 回答
1

也许您违反了外键约束。

于 2009-09-22T01:22:50.973 回答
0

对于上面的代码,只需添加对self.db.commit().

该功能远非烦恼:

当查询中出现错误时,它可以帮助您避免数据损坏问题。

于 2009-09-21T22:11:48.717 回答
0

问题可能是您没有提交更改。它可以通过 conn.commit()

在此处阅读更多信息

于 2012-11-02T06:14:42.320 回答