2012-03-19 40 views
2
 

    >>> _cursor.execute("select * from bitter.test where id > 34") 
    1L 
    >>> _cursor.fetchall() 
    ({'priority': 1L, 'default': 0, 'id': 35L, 'name': 'chinanet'},) 
    >>> _cursor.execute("select * from bitter.test where id > 34") 
    1L 
    >>> _cursor.fetchall() 
    ({'priority': 1L, 'default': 0, 'id': 35L, 'name': 'chinanet'},) 
    >>> 

第一次,我運行cursor.execute和cursor.fetchall,我得到了正確的結果。所述第二時間我運行執行和使用fetchall之前遊標從mysql取錯記錄

我插入數據到MySQL其中的ID 36,我也運行在MySQL

但commit命令cursor.execute /使用fetchall counld只能得到前的數據沒有新數據

+0

你是怎麼做到的二次插入?從Python還是MySQL shell? – 2012-03-19 04:31:57

+0

來自MySQL shell – stutiredboy 2012-03-19 04:59:29

回答

2

我猜你在使用InnoDB。這是InnoDB交易的默認值。

重複讀

這是InnoDB的默認隔離級別。對於一致的讀取, 與READ COMMITTED隔離級別存在重要區別:在同一事務中的所有一致讀取讀取第一次讀取建立的快照 。這個約定意味着如果您在同一個 事務中發出了幾個純(非鎖定)SELECT語句,則這些SELECT語句也彼此保持一致,即 。請參見第13.2.8.2節「一致性非鎖定讀取」。

我還沒有測試,但強制MySQLdb通過在當前連接上發出commit()來啓動新事務,或者創建新連接可能會解決問題。

+2

謝謝。你是對的。在fetchall之後,運行提交就OK了。或SET TRANSACTION ISOLATION LEVEL READ COMMITTED也沒關係。 – stutiredboy 2012-03-19 05:11:12

0

我嘗試這樣做,得到的結果

import MySQLdb 


conn = MySQLdb.connect('localhost', 'test', 'test', db='test') 

cur = conn.cursor() 

result = cur.execute("select * from users where id > 7") 

print "RESULT :", result 
print "DATA :", cur.fetchall() 


cur.execute("insert into users(day) values('2012-03-15')") 
conn.commit() 

result = cur.execute("select * from users where id > 7") 

print "RESULT :", result 
print "DATA :", cur.fetchall()