2016-04-27 63 views
0

在運行mariadb的環境中,以下代碼中的assert語句失敗,而在運行mysql的(舊)環境中運行良好。 mariadb環境似乎不會堅持insert語句的結果。使用命令行客戶端進行連接可確認插入未保留。Python MySQLdb在MariaDB上插入失敗:不保留到新連接

import MySQLdb 

def newCon(): 
    return MySQLdb.connect('localhost', 'root', '', 'test') 

def testStatement(con, aStatement): 
    # print "---" 
    # print aStatement 
    cur = con.cursor() 
    cur.execute(aStatement) 
    result = cur.fetchall() 
    cur.close() 
    # print result 
    return result 

def test(): 
    con = newCon() 

    testStatement(con, "DROP TABLE IF EXISTS TestTable") 
    testStatement(con, """CREATE TABLE TestTable (
      id INT NOT NULL AUTO_INCREMENT, 
      AccountId VARCHAR(50), 
      PRIMARY KEY(id))""") 
    testStatement(con, "INSERT INTO TestTable (AccountId) VALUES ('XYZ')") 
    myCount1 = testStatement(con, "SELECT count(*) from TestTable")[0][0] 

    con.close() 
    con = newCon() 

    myCount2 = testStatement(con, "SELECT count(*) from TestTable")[0][0] 

    con.close() 

    assert myCount1 == myCount2, \ 
     "count1 = " + str(myCount1) + " != count2 = " + str(myCount2) 

test() 

中運行良好的環境是: 的CentOS 6.7 蟒蛇2.6.6 的mysql-python的1.2.3 MySQL服務器73年5月1日

中失敗的環境是: Fedora的21 蟒蛇2.7.8 的mysql-python的1.2.3 MariaDB的服務器10.0.21

失敗的輸出寫着:

Test2.py:10: Warning: Table 'mysql.table_stats' doesn't exist 
    cur.execute(aStatement) 
Traceback (most recent call last): 
    File "Test2.py", line 37, in <module> 
    test() 
    File "Test2.py", line 35, in test 
    "count1 = " + str(myCount1) + " != count2 = " + str(myCount2) 
AssertionError: count1 = 1 != count2 = 0 

我不知道table_stats警告是關於什麼的。

問題:我正確使用api嗎?任何人都可以通過mariadb環境進行此測試嗎?

回答

0

您尚未提交交易。

+0

如何提交交易?這是特定於mariadb,或者我應該用mysql做到這一點嗎? – Olivier

+0

謝謝,這工作!將INSERT與BEGIN隔開 - COMMIT – Olivier

+0

如果開啓了「autocommit」,則問題將不存在。我想知道爲什麼它關閉。請參閱http://stackoverflow.com/questions/12059424/about-mysqldb-conn-autocommittrue –