import sqlite3 import pandas as pd
def initialize_database():
connection = sqlite3.connect('database(1).sqlite')
cursor = connection.cursor()cursor.execute("""
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER NOT NULL,
grade TEXT NOT NULL
)
""")cursor.executemany("""
INSERT INTO students (name, age, grade) VALUES (?, ?, ?)
""", [
("Alice", 20, "A"),
("Bob", 22, "B"),
("Charlie", 23, "A"),
("Diana", 21, "C")
])connection.commit() connection.close()
def execute_query(query, parameters=()):
connection = sqlite3.connect("example.db")
cursor = connection.cursor()
try:
cursor.execute(query, parameters)
if query.strip().upper().startswith("SELECT"):
results = cursor.fetchall()
for row in results:
print(row)
else:
connection.commit()
print("Query executed successfully.")
except sqlite3.Error as e:
print(f"An error occurred: {e}")connection.close()
def main():
initialize_database()
print("Choose a query to execute:")
print("1. View all students")
print("2. Find students by grade")
print("3. Update a student's grade")
choice = input("Enter your choice (1/2/3): ")
if choice == "1":
execute_query("SELECT * FROM students")
elif choice == "2":
grade = input("Enter the grade to filter by (A/B/C): ")
execute_query("SELECT * FROM students WHERE grade = ?", (grade,))
elif choice == "3":
student_id = int(input("Enter the student ID to update: "))
new_grade = input("Enter the new grade: ")
execute_query("UPDATE students SET grade = ? WHERE id = ?", (new_grade, student_id))
else:
print("Invalid choice!")if __name__ == "__main__":
main()