Mysql python 中的多重连接问题
Mysql multiple connection problem in python
这里我创建了两个 MySQL 到同一个数据库的连接。
当一个连接更新 class 中的数据库时,另一个连接无法获取更改。这是我的代码
tm():处理连接的数据库class,执行查询并获取数据库概览
class ClassB():
b = None
def __init__(self):
self.b = database()
def get_overview_for_b(self):
self.b.mark_invalid('9')
self.b.mark_invalid('8')
b_str = ''.join(map(str, self.b.get_overview()))
print("Getting the overview of b" + b_str)
# initializing class B
inside_class_b = ClassB()
# initializing class for A
a = database()
# get database overview for A
astart = a.get_overview()
a_str = ''.join(map(str, astart))
print("Getting the overview of a before testing" + a_str)
# updating database and get database overview for B
inside_class_b.get_overview_for_b()
# get another overview for A
aend = a.get_overview()
a_str = ''.join(map(str, aend))
print("Getting the overview of a after testing" + a_str)
# The final overview of both A and B should be same, but isn't
实际产量
Getting the overview of a before testing('PENDING', 2)
Getting the overview of b('INVALID', 2)
Getting the overview of a after testing('PENDING', 2)
预期输出
Getting the overview of a before testing('PENDING', 2)
Getting the overview of b('INVALID', 2)
Getting the overview of a after testing('INVALID', 2)
虽然我刚刚尝试过,但如果我使用 'a' 更新 'b' 获取更新值。
class ClassB():
b = None
def __init__(self):
self.b = database()
def get_overview_for_b(self):
b_str = ''.join(map(str, self.b.get_overview()))
print("Getting the overview of b" + b_str)
# initializing class B
inside_class_b = ClassB()
# initializing class for A
a = database()
# get database overview for A
astart = a.get_overview()
a_str = ''.join(map(str, astart))
print("Getting the overview of a before testing" + a_str)
# updating using 'a'
a.mark_invalid('9')
a.mark_invalid('8')
# get database overview for B
inside_class_b.get_overview_for_b()
# get another overview for A
aend = a.get_overview()
a_str = ''.join(map(str, aend))
print("Getting the overview of a after testing" + a_str)
预期输出和实际输出相同
Getting the overview of a before testing('PENDING', 2)
Getting the overview of b('INVALID', 2)
Getting the overview of a after testing('INVALID', 2)
编辑
下面是我使用的execute函数无效。这使用了一个公共连接,每次都检查 None 条件。
def execute(self, statement, attributes):
"""
Execute a query for the database
:arg:
statement - Statement to be executed.
attributes - Attributes supporting the statement.
"""
if self._db_connection is None:
self.connect()
cursor = self._db_connection.cursor()
cursor.execute(statement, attributes)
self._db_connection.commit()
t = cursor.rowcount
cursor.close()
del cursor
return t
在 get_overview() 中没有提交 command.After 添加 connection.commit() 代码按预期工作。
问题已解决。
感谢所有帮助过我的人。
这里我创建了两个 MySQL 到同一个数据库的连接。
当一个连接更新 class 中的数据库时,另一个连接无法获取更改。这是我的代码
tm():处理连接的数据库class,执行查询并获取数据库概览
class ClassB():
b = None
def __init__(self):
self.b = database()
def get_overview_for_b(self):
self.b.mark_invalid('9')
self.b.mark_invalid('8')
b_str = ''.join(map(str, self.b.get_overview()))
print("Getting the overview of b" + b_str)
# initializing class B
inside_class_b = ClassB()
# initializing class for A
a = database()
# get database overview for A
astart = a.get_overview()
a_str = ''.join(map(str, astart))
print("Getting the overview of a before testing" + a_str)
# updating database and get database overview for B
inside_class_b.get_overview_for_b()
# get another overview for A
aend = a.get_overview()
a_str = ''.join(map(str, aend))
print("Getting the overview of a after testing" + a_str)
# The final overview of both A and B should be same, but isn't
实际产量
Getting the overview of a before testing('PENDING', 2)
Getting the overview of b('INVALID', 2)
Getting the overview of a after testing('PENDING', 2)
预期输出
Getting the overview of a before testing('PENDING', 2)
Getting the overview of b('INVALID', 2)
Getting the overview of a after testing('INVALID', 2)
虽然我刚刚尝试过,但如果我使用 'a' 更新 'b' 获取更新值。
class ClassB():
b = None
def __init__(self):
self.b = database()
def get_overview_for_b(self):
b_str = ''.join(map(str, self.b.get_overview()))
print("Getting the overview of b" + b_str)
# initializing class B
inside_class_b = ClassB()
# initializing class for A
a = database()
# get database overview for A
astart = a.get_overview()
a_str = ''.join(map(str, astart))
print("Getting the overview of a before testing" + a_str)
# updating using 'a'
a.mark_invalid('9')
a.mark_invalid('8')
# get database overview for B
inside_class_b.get_overview_for_b()
# get another overview for A
aend = a.get_overview()
a_str = ''.join(map(str, aend))
print("Getting the overview of a after testing" + a_str)
预期输出和实际输出相同
Getting the overview of a before testing('PENDING', 2)
Getting the overview of b('INVALID', 2)
Getting the overview of a after testing('INVALID', 2)
编辑 下面是我使用的execute函数无效。这使用了一个公共连接,每次都检查 None 条件。
def execute(self, statement, attributes):
"""
Execute a query for the database
:arg:
statement - Statement to be executed.
attributes - Attributes supporting the statement.
"""
if self._db_connection is None:
self.connect()
cursor = self._db_connection.cursor()
cursor.execute(statement, attributes)
self._db_connection.commit()
t = cursor.rowcount
cursor.close()
del cursor
return t
在 get_overview() 中没有提交 command.After 添加 connection.commit() 代码按预期工作。
问题已解决。 感谢所有帮助过我的人。