我想复制多个数据库中的数据,处理它们,然后将它们移动到主数据库。所有的数据库都有相同的模式。Peewee ORM - 复制主数据库中的多个数据库中的数据
即使我打开和关闭数据库,peewee总是连接到同一个数据库(列表中的第三个),并且不尊重连接顺序。
databases = [spie_db, opticsorg_db, phcom_db]
# map to a dictionary the values from the record
def mapping(record):
comp_d = {'name': record.name,
'address': record.address,
'country': record.country,
'website': record.website,
'domain': record.domain
}
return comp_d
def merge_data():
company_list = []
for database in databases:
database.connect()
# cycle trough db
for record in Company.select():
# append each record to the list
company_list.append(mapping(record))
database.close()
return company_list
# get data from the other databases
companies = merge_data()
# the merge database
db.connect()
# add records in the merge db
for company in companies:
Company.create(name=company['name'], address=company['address'], country=company['country'],
website=company['website'], domain=company['domain'])
db.close()