Last active
February 26, 2019 09:15
-
-
Save msabramo/8319097 to your computer and use it in GitHub Desktop.
pymssql: Demonstration that with as_dict=True you have to name result columns that come from functions like MIN/MAX
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
(py26.venv)marca@marca-mac2:~/dev/git-repos/pymssql$ python stuff/crisp2.py | |
SELECT MAX(x) | |
FROM (VALUES (1, 2), (3, 4), (5, 6), (7, 8)) AS foo(x, y) | |
rows1 = [{}] | |
SELECT MAX(x) AS [MAX(x)] | |
FROM (VALUES (1, 2), (3, 4), (5, 6), (7, 8)) AS foo(x, y) | |
rows2 = [{u'MAX(x)': 7}] | |
SELECT MAX(x) | |
FROM (VALUES (1, 2), (3, 4), (5, 6), (7, 8)) AS foo(x, y) | |
rows1 = [(7,)] |
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
from os import getenv | |
import pymssql | |
database_host = getenv("PYMSSQL_TEST_SERVER") | |
database_user = getenv("PYMSSQL_TEST_USERNAME") | |
user_password = getenv("PYMSSQL_TEST_PASSWORD") | |
database_name = getenv("PYMSSQL_TEST_DATABASE") | |
conn = pymssql.connect( | |
host=database_host, | |
user=database_user, | |
password=user_password, | |
database=database_name, | |
as_dict=True) | |
cur = conn.cursor() | |
sql = """ | |
SELECT MAX(x) | |
FROM (VALUES (1, 2), (3, 4), (5, 6), (7, 8)) AS foo(x, y) | |
""" | |
cur.execute(sql) | |
rows1 = cur.fetchall() | |
print(sql) | |
print("rows1 = %r" % rows1) | |
sql = """ | |
SELECT MAX(x) AS [MAX(x)] | |
FROM (VALUES (1, 2), (3, 4), (5, 6), (7, 8)) AS foo(x, y) | |
""" | |
cur.execute(sql) | |
rows2 = cur.fetchall() | |
print(sql) | |
print("rows2 = %r" % rows2) | |
conn = pymssql.connect( | |
host=database_host, | |
user=database_user, | |
password=user_password, | |
database=database_name, | |
as_dict=False) | |
cur = conn.cursor() | |
sql = """ | |
SELECT MAX(x) | |
FROM (VALUES (1, 2), (3, 4), (5, 6), (7, 8)) AS foo(x, y) | |
""" | |
cur.execute(sql) | |
rows1 = cur.fetchall() | |
print(sql) | |
print("rows1 = %r" % rows1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment