Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python to Query multiple SQL databases on different servers

I have been doing a fair amount of manual data analysis, reporting and dash boarding recently via SQL and wonder if perhaps python would be able to automate a lot of this. I am not familiar with Python at all so I hope my question makes sense. For security/performance issues, we store databases on a number of servers (more than 5) which contain data that would be pertinent to a query. Unfortunately, these servers are set up so they cannot talk to each other so I cant pull data from the two servers in the same query. I believe this is a limitation due to using windows credentials/security.

For my data analysis and reporting needs, I need to be able to grab pertinent data from two or more of these so the way I currently do this is by running a query, grabbing the results, running another query with the results, doing some formula work in excel, and then running another query and so on and so forth until I get what I need.

Unfortunately this both time consuming, and also makes me pull massive datasets (in the multiple millions of rows), which I then have to continually narrow down based on criteria that are in said databases.

I know Python has the ability to query SQL Server, however I figured I would ask the experts:

Can I manipulate the data in the background with Python similar to how I can do with excel (lookups, statistical functions, etc, perhaps even XML/webAPI?

Can Python handle connections to multiple different database servers at the same time?

Does Python handle windows credentials well?

If Python is not the tool for this, can you name one that would work better?

Please let me know if I can provide additional pertinent details.

Ideally, I would like to end up creating our own separate database and creating automated processes to pull everything from other databases but currently that is not possible due to project constraints.

Thanks!

like image 555
christodorov Avatar asked Nov 18 '25 14:11

christodorov


1 Answers

I didn't use windows credential. But i have used Python to work with multiple MS-SQL databases at the same time. It worked very well. You can use the library pymssql or better with SQLAlchemy

But i think you should start with a basic tutorial about Python first. Because you want to work with millions of rows, it's very important to understand list, set, tuple, dict in Python. For good performance, you should use the right type.

A basic example with pymssql

import pymssql

conn1 = pymssql.connect("Host1", "user1", "password1", "db1")
conn2 = pymssql.connect("Host2", "user2", "password2", "db2")

cursor1 = conn1.cursor()
cursor2 = conn2.cursor()

cursor1.execute('SELECT * FROM TABLE1 LIMIT 10')
cursor2.execute('SELECT * FROM TABLE2 LIMIT 10')

result1 = cursor1.fetchall()
result2 = cursor2.fetchall()

# print each row
for row in result1:
   print(row)

# print each row
for row in result2:
   print(row)
like image 173
qvpham Avatar answered Nov 21 '25 04:11

qvpham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!