forked from anthonydb/python-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyodbc-test.py
43 lines (33 loc) · 1.11 KB
/
pyodbc-test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Example of using pyodbc to connect to server, fetch data,
# and return it as JSON or print to screen
# module import
import pyodbc
import json
# server-specific connection string
connstr = 'DRIVER={SQL Server};SERVER=ServerName;DATABASE=Test;'
# use pyodbc's "connect" method to establish the connection
conn = pyodbc.connect(connstr)
# create a cursor object using the connection's "cursor" method
cursor = conn.cursor()
# two variables to limit the query
fname = 'Bob'
lname = 'Jones'
# execute the query, passing in the variables
cursor.execute("""
SELECT ID, LastName, FirstName
FROM Employees
WHERE LastName = ? AND FirstName = ?
""", lname, fname)
# the variable "rows" contains every row fetched
rows = cursor.fetchall()
# now, build a list of lists to output to JSON
outlist = []
for row in rows:
l = [row.FirstName, row.LastName, str(row.ID)]
outlist.append(l)
#print row.FirstName + ' ' + row.LastName + ', ID: ' + str(row.ID)
# dump the list to JSON and print to screen.
j = json.dumps(outlist)
print j
# close the connection to the server
conn.close()