SQLAlchemy Python 查找和插入

2024-03-29 12:06:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下几点:

from sqlalchemy import create_engine

engine1 = create_engine('mysql://user:password@host1/schema', echo=True)
engine2 = create_engine('mysql://user:password@host2/schema')

connection1 = engine1.connect()
connection2 = engine2.connect()

table1 = connection1.execute("select * from table1")
table2 = connection2.execute("select * from table2")

现在我想把这个table1中的所有条目插入connection2中相同的空表table2。你知道吗

我怎样才能做到?你知道吗

我还可以从table1中创建一个dict,然后将它插入table2。正如我从sqlalchemy的文档中了解到的,有一种方法可以做到这一点,但是这里的示例假设您创建一个全新的表,以便使用new_table.insert()插入其中。它对我现有的表不起作用。你知道吗

谢谢


Tags: fromsqlalchemyschemaconnectcreatemysqlpasswordengine