Niuya
Niuya

Reputation: 424

How to concat 2 or more sqlalchemy rowproxy

I have 2 tables with same structure but in different db instance & schema. So I run 2 queries:

rows1 = conn1.execute(query1)
rows2 = conn2.execute(query2)

is there any magic to concat/combine these 2 result?(rows1 & row2)

is it possible like list: rows = rows1 + rows2 ?

Upvotes: 0

Views: 52

Answers (1)

snakecharmerb
snakecharmerb

Reputation: 55903

Queries over different databases cannot be UNIONed, so any solution must be in the application layer.

The simplest solution is to materialise both resultsets as lists and combine them:

combined_rows = list(rows1) + list(rows2)
for row in combined_rows:
    # do stuff with row

If materialising the lists is not desirable you could use itertools.chain:

for row in itertools.chain(rows1, rows2):
    # do stuff with row

Upvotes: 1

Related Questions