Python Database Software

This is a python database program which adds,reads and delete data.It uses SQLite3 rdbms which is builtin to python.Its Tkinter for UI.Good for beginners for learning.to understand you need to know a little db coding and also python gaphics also.Use VS Code ,Visual Studio or pyCharm for this pogram.You can add view and delete user name and email .Taken from AI.

import sqlite3

import tkinter as tk

from tkinter import messagebox, ttk



# --- DATABASE SETUP ---

def init_db():

    """Initializes the SQLite database and creates the users table."""

    conn = sqlite3.connect("users_data.db")

    cursor = conn.cursor()

    cursor.execute(

        """

        CREATE TABLE IF NOT EXISTS users (

            id INTEGER PRIMARY KEY AUTOINCREMENT,

            name TEXT NOT NULL,

            email TEXT NOT NULL UNIQUE

        )

    """

    )

    conn.commit()

    conn.close()



# --- DATABASE OPERATIONS (CRUD) ---

def add_user():

    name = name_entry.get().strip()

    email = email_entry.get().strip()


    if not name or not email:

        messagebox.showerror("Error", "All fields are required!")

        return


    try:

        conn = sqlite3.connect("users_data.db")

        cursor = conn.cursor()

        cursor.execute(

            "INSERT INTO users (name, email) VALUES (?, ?)", (name, email)

        )

        conn.commit()

        conn.close()

        messagebox.showinfo("Success", "User added successfully!")

        clear_entries()

        view_users()

    except sqlite3.IntegrityError:

        messagebox.showerror("Error", "This email is already registered.")



def view_users():

    # Clear existing data in the table view

    for row in tree.get_children():

        tree.delete(row)


    conn = sqlite3.connect("users_data.db")

    cursor = conn.cursor()

    cursor.execute("SELECT * FROM users")

    rows = cursor.fetchall()

    conn.close()


    for row in rows:

        tree.insert("", tk.END, values=row)

        #messagebox.showwarning("Warning",type(row))



def delete_user():

    selected_item = tree.selection()

    if not selected_item:

        messagebox.showwarning("Warning", "Please select a user to delete.")

        return


    user_id = tree.item(selected_item)["values"][0]

    

    conn = sqlite3.connect("users_data.db")

    cursor = conn.cursor()

    cursor.execute("DELETE FROM users WHERE id = ?", (user_id,))

    conn.commit()

    conn.close()


    #messagebox.showinfo("Success", "User deleted successfully!")

    view_users()



def clear_entries():

    name_entry.delete(0, tk.END)

    email_entry.delete(0, tk.END)



# --- UI SETUP ---

init_db()  # Setup database on launch


root = tk.Tk()

root.title("Data Intigo")

root.geometry("500x400")


# Input Form

form_frame = tk.Frame(root, pady=10)

form_frame.pack()


tk.Label(form_frame, text="Name:").grid(row=0, column=0, sticky="w", padx=5)

name_entry = tk.Entry(form_frame, width=30)

name_entry.grid(row=0, column=1, padx=5, pady=5)


tk.Label(form_frame, text="Email:").grid(row=1, column=0, sticky="w", padx=5)

email_entry = tk.Entry(form_frame, width=30)

email_entry.grid(row=1, column=1, padx=5, pady=5)


# Action Buttons

btn_frame = tk.Frame(root)

btn_frame.pack(pady=10)


add_btn = tk.Button(

    btn_frame, text="Add User", command=add_user, width=12, bg="green", fg="white"

)

add_btn.grid(row=0, column=0, padx=5)


delete_btn = tk.Button(

    btn_frame,

    text="Delete Selected",

    command=delete_user,

    width=12,

    bg="red",

    fg="white",

)

delete_btn.grid(row=0, column=1, padx=5)


# Data Display Table (Treeview)

tree_frame = tk.Frame(root)

tree_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=10)


columns = ("ID", "Name", "Email")

tree = ttk.Treeview(tree_frame, columns=columns, show="headings")

tree.heading("ID", text="ID")

tree.heading("Name", text="Name")

tree.heading("Email", text="Email")


# Format columns

tree.column("ID", width=50, anchor="center")

tree.column("Name", width=150)

tree.column("Email", width=250)

tree.pack(fill=tk.BOTH, expand=True)


# Initial load of data

view_users()


root.mainloop()


Comments

Popular posts from this blog

Do Muslims worship the same God as the Christians and Jews?