我正在使用 MySQL 连接器/Python。我有一个生成对象的对象,其中一些对象生成更多对象。目前,我最终得到了大约 300 个对象。鉴于我设置它的方式,这意味着 300 个数据库连接。是否可以连接一次并简单地传递连接?
我设想它看起来像这样:
class MyObject(dict):
def __init__(self, row, conn):
self.conn = conn
self.cursor = self.conn.cursor()
# pass the connection/cursor on to some more objects
def getObjects(query, args):
conn = mysql.connector.connect(user="user", password="password", host="localhost", port="12345", database="database")
cursor = conn.cursor()
cursor.execute(query, args)
row = cursor.fetchone()
myObjects = []
while rowDict is not None:
myObj = MyObject(row, conn)
myObjects.append(myObj)
row = cursor.fetchone()
cursor.close()
conn.close()
return myObjects
但是当我以这种方式实现它时,我得到InternalError: Unread result found.
.