The problem with closing the database connection

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật