forked from aiwithqasim/sqlserver-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (60 loc) · 2.01 KB
/
Copy pathmain.py
File metadata and controls
70 lines (60 loc) · 2.01 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import logging
import sys
from utils.connect import create_connection
from src.setup_database import setup_database
from src.insert import insert_author
from src.update import update_author
from src.procedures import call_stored_procedure
from src.config.config import SQL_SELECT_ALL_AUTHORS
# Configure logging
logging.basicConfig(
stream=sys.stdout,
encoding='utf-8',
format='%(levelname)s:%(message)s',
level=logging.INFO
)
def main():
# Set up the database schema first
if not setup_database():
logging.error("Database setup failed, cannot proceed")
return
# Insert a sample author
author_id = insert_author("John", "Doe", "1970-01-01")
if author_id:
logging.info(f"Successfully inserted author with ID: {author_id}")
else:
logging.error("Failed to insert author")
return
# Update the author
update_author(1, "Jane", "Smith", "1981-01-01")
# Display all authors in the database
display_authors()
# Call the stored procedure
result = call_stored_procedure("GetAuthorById", 1)
logging.info(f"Stored procedure result: {result}")
def display_authors():
"""Query and display all authors in the database"""
conn = create_connection()
if not conn:
logging.error("Failed to connect to database for querying authors")
return
cursor = None
try:
cursor = conn.cursor()
cursor.execute(SQL_SELECT_ALL_AUTHORS)
authors = cursor.fetchall()
if authors:
logging.info(f"Found {len(authors)} authors in database:")
for author in authors:
logging.info(f" ID: {author[0]}, Name: {author[1]} {author[2]}, Birth Date: {author[3]}")
else:
logging.info("No authors found in database")
except Exception as e:
logging.error(f"Error querying authors: {e}")
finally:
if cursor:
cursor.close()
if conn:
conn.close()
if __name__ == "__main__":
main()