Created
October 21, 2015 18:35
-
-
Save rafaelhenrique/2c91d4a747e7cd96ec1a to your computer and use it in GitHub Desktop.
Simple use pymssql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding utf-8 -*- | |
import pymssql | |
SQL_SERVER = { | |
'server': '127.0.0.1:1433', | |
'user': 'myuser', | |
'password': 'mypass', | |
'database': 'mydatabase', | |
} | |
QUERY = """ | |
SELECT * | |
FROM MYTABLE(nolock) | |
WHERE COLUMN={0} | |
""" | |
def fetch_sqlserver_select(config, query): | |
conn = pymssql.connect(**config) | |
cursor = conn.cursor(as_dict=True) | |
cursor.execute(query) | |
for row in cursor: | |
yield row | |
cursor.close() | |
conn.close() | |
my_rows = list(fetch_sqlserver_select(SQL_SERVER,QUERY.format("RAFAEL"))) | |
print(rows) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment