0% found this document useful (0 votes)
3 views

Dev Cs Project

This document contains a Python script for managing a chemistry database using SQLite. It allows users to create a table for chemical elements, add new elements, search for elements by atomic number or symbol, display all elements, delete elements, and export the data to a CSV file. The script includes a main menu for user interaction and handles various operations related to the database.

Uploaded by

jnvg893
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Dev Cs Project

This document contains a Python script for managing a chemistry database using SQLite. It allows users to create a table for chemical elements, add new elements, search for elements by atomic number or symbol, display all elements, delete elements, and export the data to a CSV file. The script includes a main menu for user interaction and handles various operations related to the database.

Uploaded by

jnvg893
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import sqlite3

import csv

def connect_db():
conn = sqlite3.connect("chemistry.db")
return conn

def create_table():
conn = connect_db()
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS
Elements (
atomic_number INTEGER PRIMARY KEY,
name TEXT,
symbol TEXT,
atomic_mass REAL
)''')
conn.commit()
conn.close()

def add_element(atomic_number, name, symbol, atomic_mass):


conn = connect_db()
cursor = conn.cursor()
try:
cursor.execute("INSERT INTO Elements VALUES (?, ?, ?, ?)",
(atomic_number, name, symbol, atomic_mass))
conn.commit()
print(f"Element '{name}' added successfully.")
except sqlite3.IntegrityError:
print(f"Element with atomic number {atomic_number} already exists.")
conn.close()

def search_element(search_by, value):


conn = connect_db()
cursor = conn.cursor()
if search_by == "atomic_number":
cursor.execute("SELECT * FROM Elements WHERE atomic_number = ?", (value,))
elif search_by == "symbol":
cursor.execute("SELECT * FROM Elements WHERE symbol = ?", (value,))
element = cursor.fetchone()
conn.close()
if element:
print(f"\nElement Found:")
print(f"Atomic Number: {element[0]}, Name: {element[1]}, Symbol:
{element[2]}, Atomic Mass: {element[3]}")
else:
print("Element not found.")

def display_elements():
conn = connect_db()
cursor = conn.cursor()
cursor.execute("SELECT * FROM Elements ORDER BY atomic_number")
elements = cursor.fetchall()
conn.close()
print("\nAll Elements in the Database:")
print(f"{'Atomic Number':<15} {'Name':<20} {'Symbol':<10} {'Atomic Mass':<10}")
print("-" * 60)
for element in elements:
print(f"{element[0]:<15} {element[1]:<20} {element[2]:<10}
{element[3]:<10}")

def delete_element(atomic_number):
conn = connect_db()
cursor = conn.cursor()
cursor.execute('''DELETE FROM Elements WHERE atomic_number = ?''',
(atomic_number,))
conn.commit()
conn.close()
print(f"Element with atomic number {atomic_number} deleted successfully.")

def export_to_file(filename):
conn = connect_db()
cursor = conn.cursor()
cursor.execute("SELECT * FROM Elements")
elements = cursor.fetchall()
conn.close()

with open(filename, "w", newline="") as file:


writer = csv.writer(file)
writer.writerow(["Atomic Number", "Name", "Symbol", "Atomic Mass"])
writer.writerows(elements)
print(f"Data exported to {filename} successfully.")

def main():
create_table()
while True:
print("\n--- Chemistry Database Management ---")
print("1. Add Element")
print("2. Search Element by Atomic Number")
print("3. Search Element by Symbol")
print("4. Display All Elements")
print("5. Export Data to File")
print("6. Delete an element")
print("7. Exit")

choice = input("Enter your choice: ")


if choice == "1":
try:
atomic_number = int(input("Enter Atomic Number: "))
name = input("Enter Name: ")
symbol = input("Enter Symbol: ")
atomic_mass = float(input("Enter Atomic Mass: "))
add_element(atomic_number, name, symbol, atomic_mass)
except ValueError:
print("Invalid input! Please enter numeric values for atomic number
and atomic mass.")

elif choice == "2":


try:
atomic_number = int(input("Enter Atomic Number: "))
search_element("atomic_number", atomic_number)
except ValueError:
print("Please enter a valid atomic number.")

elif choice == "3":


symbol = input("Enter Symbol: ")
search_element("symbol", symbol)

elif choice == "4":


display_elements()

elif choice == "5":


filename = input("Enter filename (with .csv extension): ")
export_to_file(filename)

elif choice=='6':
j=int(input("Enter the element's atomic no. u want to delete:"))
delete_element(j)

elif choice == "7":


print("Exiting the program. Goodbye!")
break

else:
print("Invalid choice! Please try again.")

if __name__ == "__main__":
main()

You might also like