Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@kingkastle

This comment has been minimized.

Copy link

@kingkastle kingkastle commented Mar 31, 2015

Nice! Thanks for sharing

@sjl070707

This comment has been minimized.

Copy link

@sjl070707 sjl070707 commented Oct 23, 2015

awesome, thank you

@mullenkamp

This comment has been minimized.

Copy link

@mullenkamp mullenkamp commented May 26, 2016

Thank you very much!

@cbcoutinho

This comment has been minimized.

Copy link

@cbcoutinho cbcoutinho commented Sep 23, 2016

Exactly what I was looking for - thanks!

@drorata

This comment has been minimized.

Copy link

@drorata drorata commented Apr 20, 2017

Boom! Thanks!

@drorata

This comment has been minimized.

Copy link

@drorata drorata commented Apr 20, 2017

The only downside is that you have to provide the password in the code. Not very nice.

@jbkrege

This comment has been minimized.

Copy link

@jbkrege jbkrege commented Jun 15, 2017

@drorata you could always have a line like
password = input("Enter password")
if you're averse to hard-coding it

@jbkrege

This comment has been minimized.

Copy link

@jbkrege jbkrege commented Jun 15, 2017

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

@ArthurCamara

This comment has been minimized.

Copy link

@ArthurCamara ArthurCamara commented Feb 6, 2018

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

@ctivanovich

This comment has been minimized.

Copy link

@ctivanovich ctivanovich commented Jun 26, 2018

Or use a config file and the configparser module.

@savioserra

This comment has been minimized.

Copy link

@savioserra 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

This comment has been minimized.

Copy link

@mymindwentblvnk mymindwentblvnk commented Dec 11, 2018

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

@superposition

This comment has been minimized.

Copy link

@superposition superposition commented Jan 25, 2019

you could just beam the password into the computer

@priyaranjan-tokachichu

This comment has been minimized.

Copy link

@priyaranjan-tokachichu priyaranjan-tokachichu commented Jul 27, 2019

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

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