-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthor_operations.py
52 lines (44 loc) · 1.91 KB
/
author_operations.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
44
45
46
47
48
49
50
51
52
def add_author(cursor, conn):
name = input("Enter author name: ")
biography = input("Enter author biography: ")
try:
# Check if author already exists
cursor.execute("SELECT * FROM authors WHERE name = %s", (name,))
existing_author = cursor.fetchone()
if existing_author:
print("Author already exists.")
return
# Insert the new author into the database
cursor.execute("INSERT INTO authors (name, biography) VALUES (%s, %s)", (name, biography))
conn.commit()
print("Author added successfully!")
except mysql.connector.Error as err:
print(f"Error adding author: {err}")
conn.rollback()
def view_author_details(cursor):
name = input("Enter author name: ")
try:
# Fetch author details from the database
cursor.execute("SELECT name, biography FROM authors WHERE name = %s", (name,))
result = cursor.fetchone()
if result:
name, biography = result
print(f"Name: {name}")
print(f"Biography: {biography}")
# Fetch books written by this author
cursor.execute("SELECT title FROM books WHERE author_id = (SELECT id FROM authors WHERE name = %s)", (name,))
books_written = [row[0] for row in cursor.fetchall()]
print(f"Books Written: {', '.join(books_written) if books_written else 'None'}")
else:
print("Author not found.")
except mysql.connector.Error as err:
print(f"Error viewing author details: {err}")
def display_all_authors(cursor):
try:
# Fetch all authors from the database
cursor.execute("SELECT name, biography FROM authors")
results = cursor.fetchall()
for name, biography in results:
print(f"Name: {name}, Biography: {biography}")
except mysql.connector.Error as err:
print(f"Error displaying authors: {err}")