There is some issue with closing the database connection, but I don’t understand why I can’t close the access and delete the database afterwards. I tried a lot of time, but I didn`t see information about my error.
I tried to describe the problem with deletion in the delete_database function, but it didn’t work out. The error that appears is:
Exception ignored in atexit callback: <function delete_database at 0x0000023EA47A8720>
Traceback (most recent call last):
File "File name", line 71, in delete_database
os.remove("University.db")
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'University.db
My code
import sqlite3
import os
def create_tables():
with sqlite3.connect("University.db") as database:
cursor = database.cursor()
# Створення таблиць
cursor.execute("""CREATE TABLE IF NOT EXISTS Students (
student_id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT,
age INTEGER,
group_id TEXT,
FOREIGN KEY (group_id) REFERENCES Groups(group_id)
)""")
cursor.execute("""CREATE TABLE IF NOT EXISTS Groups (
group_id TEXT PRIMARY KEY,
group_name TEXT
)""")
cursor.execute("""CREATE TABLE IF NOT EXISTS Courses (
course_id INTEGER PRIMARY KEY,
course_name TEXT
)""")
cursor.execute("""CREATE TABLE IF NOT EXISTS Teachers (
teacher_id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT
)""")
cursor.execute("""CREATE TABLE IF NOT EXISTS Results (
result_id INTEGER PRIMARY KEY,
student_id INTEGER,
subject_id INTEGER,
teacher_id INTEGER,
grade INTEGER,
FOREIGN KEY (student_id) REFERENCES Students(student_id),
FOREIGN KEY (subject_id) REFERENCES Subjects(subject_id),
FOREIGN KEY (teacher_id) REFERENCES Teachers(teacher_id)
)""")
database.commit()
def add_student(first_name, last_name, age, group_id):
with sqlite3.connect("University.db") as database:
cursor = database.cursor()
cursor.execute("INSERT INTO Students (first_name, last_name, age, group_id) VALUES (?, ?, ?, ?)",
(first_name, last_name, age, group_id))
database.commit()
def get_student_info(student_id):
with sqlite3.connect("University.db") as database:
cursor = database.cursor()
cursor.execute("SELECT * FROM Students WHERE student_id = ?", (student_id,))
student_info = cursor.fetchone()
return student_info
def get_all_students():
with sqlite3.connect("University.db") as database:
cursor = database.cursor()
cursor.execute("SELECT * FROM Students")
all_students = cursor.fetchall()
return all_students
def delete_database():
if os.path.exists("University.db"):
os.remove("University.db")
print("The database file has been deleted.")
else:
print("The database file does not exist.")
atexit.register(delete_database)
create_tables()
while True:
option = int(input("Select an option:n1. Information about one studentn2. Information about all studentsn3. Exit"))
if option == 1:
student_id = input("Enter the student number: ")
student_info = get_student_info(student_id)
print(student_info)
elif option == 2:
all_students = get_all_students()
for student in all_students:
print(student)
elif option == 3:
break
else:
print("Incorrect. Try again!")
New contributor
Олег is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.