<p>您可能需要检查数据库的事务隔离级别。如果设置为REPEATABLE-READ,那么您描述的行为就是您所期望的。您可能想将其更改为READ-COMMITTED。</p>
<p>由于问题的原始海报提到他只是在查询数据库,所以不能忘记提交。插入提交似乎是一种解决方法,因为它会导致一个新的事务开始;并且可能需要建立一个新的快照。不过,对我来说,在每次选择之前插入commit并不是一个好的编程实践。</p>
<p>由于解决方案在于正确配置数据库,因此没有要显示的python代码。</p>
<p><strong>在<a href="http://dev.mysql.com/doc/refman/5.5/en/set-transaction.html">http://dev.mysql.com/doc/refman/5.5/en/set-transaction.html</a>查看MySQL文档。</p>
<blockquote>
<p>REPEATABLE READ <br/>
This is the default isolation level for InnoDB. For consistent reads, there is an important difference from the READ COMMITTED isolation level: All consistent reads within the same transaction read the snapshot established by the first read. This convention means that if you issue several plain (nonlocking) SELECT statements within the same transaction, these SELECT statements are consistent also with respect to each other. See Section 14.3.9.2, “Consistent Nonlocking Reads”. <br/><br/></p>
<p>READ COMMITTED <br/>
A somewhat Oracle-like isolation level with respect to consistent (nonlocking) reads: Each consistent read, even within the same transaction, sets and reads its own fresh snapshot. See Section 14.3.9.2, “Consistent Nonlocking Reads”. <br/><br/></p>
</blockquote>
<p>检查配置的隔离级别:</p>
<pre><code>>mysql > SELECT @@GLOBAL.tx_isolation, @@tx_isolation;
+-----------------------+-----------------+
| @@GLOBAL.tx_isolation | @@tx_isolation |
+-----------------------+-----------------+
| REPEATABLE-READ | REPEATABLE-READ |
+-----------------------+-----------------+
1 row in set (0.01 sec)
</code></pre>
<p>将事务隔离级别设置为READ-COMMITTED</p>
<pre><code>mysql> SET GLOBAL tx_isolation='READ-COMMITTED';
Query OK, 0 rows affected (0.00 sec)
mysql> SET SESSION tx_isolation='READ-COMMITTED';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @@GLOBAL.tx_isolation, @@tx_isolation;
+-----------------------+----------------+
| @@GLOBAL.tx_isolation | @@tx_isolation |
+-----------------------+----------------+
| READ-COMMITTED | READ-COMMITTED |
+-----------------------+----------------+
1 row in set (0.01 sec)
mysql>
</code></pre>
<p>再次运行应用程序</p>