0

The assert statement in the following code fails in an environment running mariadb, while it runs fine in an (older) environment running mysql. The mariadb environment seems to not persist the result of the insert statement. Connecting with a command line client confirms that the insert was not persisted.

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

The environment in which it runs fine is: CentOS 6.7 python 2.6.6 mysql-python 1.2.3 mysql server 5.1.73

The environment in which it fails is: Fedora 21 python 2.7.8 mysql-python 1.2.3 mariadb server 10.0.21

The failed output reads:

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

I don't know what the table_stats warning is about.

Question: Am I using the api correctly? Can anybody make this test pass with a mariadb environment?

Olivier
  • 118
  • 2
  • 8

1 Answers1

1

You haven't committed the transaction.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895