Python MariaDB 上的 MySQLdb 插入失败:不会持续到新连接
Python MySQLdb Insert fails on MariaDB: Doesn't persist to new connection
以下代码中的 assert 语句在环境 运行 mariadb 中失败,而在(较旧的)环境 运行 mysql 中运行良好。 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()
正常运行的环境是:
中央操作系统 6.7
python 2.6.6
mysql-python 1.2.3
mysql 服务器 5.1.73
失败的环境是:
软呢帽 21
python 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 环境下通过这个测试吗?
您还没有提交交易。
以下代码中的 assert 语句在环境 运行 mariadb 中失败,而在(较旧的)环境 运行 mysql 中运行良好。 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()
正常运行的环境是: 中央操作系统 6.7 python 2.6.6 mysql-python 1.2.3 mysql 服务器 5.1.73
失败的环境是: 软呢帽 21 python 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 环境下通过这个测试吗?
您还没有提交交易。