-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.py
30 lines (24 loc) · 789 Bytes
/
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
import pyodbc as po
# Connection variables
server = ''
database = ''
username = ''
password = ''
# Connection string
cnxn = po.connect('DRIVER={ODBC Driver 18 for SQL Server};SERVER=' +
server+';DATABASE='+database+';UID='+username+';PWD=' + password+';TrustServerCertificate=yes;')
cursor = cnxn.cursor()
# Fetch data into a cursor
cursor.execute("SELECT [Id],[FirstName],[LastName],[Address] FROM [TestDatabase].[dbo].[Personal] \
ORDER BY Id DESC;")
# iterate the cursor
row = cursor.fetchone()
while row:
# Print the row
print(str(row[0]) + ", " + str(row[1] or '') + ", " + str(row[2] or '') + ", " + str(row[3] or ''))
row = cursor.fetchone()
# Close the cursor and delete it
cursor.close()
del cursor
# Close the database connection
cnxn.close()