数据库
首页 > 数据库> > Python MySQL-选择有效,但不能删除?

Python MySQL-选择有效,但不能删除?

作者:互联网

我是PythonPython的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))

有任何想法吗?

解决方法:

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

查看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.

另请参阅类似的SO问题:Python MySQLdb update query fails

标签:python,mysql,sql-delete,select
来源: https://codeday.me/bug/20191010/1887544.html