-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaxdatabase_retrieving_data.py
More file actions
38 lines (30 loc) · 942 Bytes
/
Copy pathtaxdatabase_retrieving_data.py
File metadata and controls
38 lines (30 loc) · 942 Bytes
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
import pyodbc
# Connection details
server = 'servername'
database = 'databasename'
username = 'admin'
password = 'password'
driver = '{ODBC Driver 17 for SQL Server}'
try:
# Connecting to the database
print("Connecting to the database...")
conn = pyodbc.connect(
f"DRIVER={driver};SERVER={server};DATABASE={database};UID={username};PWD={password}"
)
print("Connection established!")
# Creating a cursor object to execute queries
cursor = conn.cursor()
# SQL query to select data
query = "SELECT TOP 5 * FROM DimTravel;" # Example: Retrieve the first 5 rows from DimTravel
cursor.execute(query)
# Fetching the data
rows = cursor.fetchall()
# Printing the results
print("Here are the results:")
for row in rows:
print(row)
# Closing the connection
conn.close()
print("Connection closed.")
except Exception as e:
print("An error occurred:", e)