Skip to content

Instantly share code, notes, and snippets.

@hunterowens
Last active December 9, 2023 16:27
Show Gist options
  • Save hunterowens/08ebbb678255f33bba94 to your computer and use it in GitHub Desktop.
Save hunterowens/08ebbb678255f33bba94 to your computer and use it in GitHub Desktop.
Pandas and MSSQL
import pymssql
import pandas as pd
## instance a python db connection object- same form as psycopg2/python-mysql drivers also
conn = pymssql.connect(server="172.0.0.1", user="howens",password="some_fake_password", port=63642) # You can lookup the port number inside SQL server.
## Hey Look, college data
stmt = "SELECT * FROM AlumniMirror..someTable"
# Excute Query here
df = pd.read_sql(stmt,conn)
df.head(5)
pymssql==2.1.0
pandas==0.14.0
@ArthurCamara
Copy link

Maybe the "Right" way would be to make the password an environment variable?

@ctivanovich
Copy link

Or use a config file and the configparser module.

@savioserra
Copy link

savioserra commented Jul 19, 2018

Hi! I would sugest you to wrap the conn variable inside a with statement as described in the docs! 😃

@mymindwentblvnk
Copy link

Do you by chance know how to make this work with SQLAlchemy?

@superposition
Copy link

you could just beam the password into the computer

@priyaranjan-tokachichu
Copy link

correction: 'raw_input()' not 'input()'

Actually, you are correct the first time. It is now input().
https://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x

@mriostamez
Copy link

Awesome, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment