Skip to content

Instantly share code, notes, and snippets.

@rafaelhenrique
Created October 21, 2015 18:35
Show Gist options
  • Save rafaelhenrique/2c91d4a747e7cd96ec1a to your computer and use it in GitHub Desktop.
Save rafaelhenrique/2c91d4a747e7cd96ec1a to your computer and use it in GitHub Desktop.
Simple use pymssql
# -*- 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