PYODBC, what is the most efficient algorithm for pulling data from two databases in my case?

Wednesday, July 9, 2014

So what I need to do is currently pull data row by row (or a better method?) from two or more databases. Originally, I had something like this:



overall_list = []
customer_list = []
if remote_system_id == 'db1' or remote_system_id is None
db1_cursor.execute(db1_full_SQLstring, db1_parameters)
row = db1_cursor.fetchone()
while row:
customer_list.append(str(row[0]))
row = db1_cursor.fetchone()
overall_list.append(customer_list)

customer_list = []
if remote_system_id == 'db2' or remote_system_id is None
db2_cursor.execute(db2_full_SQLstring, db2_parameters)
row = db2_cursor.fetchone()
while row:
customer_list.append(str(row[0]))
row = db2_cursor.fetchone()
overall_list.append(customer_list)


All the way to db*n* (likely at most 3 or 4). Then I changed it to this:



customer_list = []
db1_cursor.execute(db1_full_SQLstring, SQLStrings[1])
db2_cursor.execute(db2_full_SQLstring, SQLStrings[3])
row = 1
row2 = 1
while row or row2:
if db1_cursor.fetchone() is not None:
customer_list.append(str(row[0]))
row = db1_cursor.fetchone()
if db2_cursor.fetchone() is not None:
customer_list.append(str(row2[0]))
row2 = db2_cursor.fetchone()


I've come to realize that I made the code worse by updating it the second time. I'm wondering if there's a better way of doing this. My goal is to not have two separate routines fetching data from the databases. Though, I'm open to all ideas.


Thanks!







http://ift.tt/1k5andG