Spaces:
Runtime error
Runtime error
File size: 2,386 Bytes
802735c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
import sqlite3
import streamlit as st
def create_database():
conn = sqlite3.connect('customers.db')
c = conn.cursor()
c.execute("""
SELECT name FROM sqlite_master WHERE type='table' AND name='customers'
""")
if not c.fetchone():
c.execute('''CREATE TABLE customers
(name text, address text, phone text)''')
conn.commit()
conn.close()
def add_customer(name, address, phone):
conn = sqlite3.connect('customers.db')
c = conn.cursor()
c.execute("INSERT INTO customers VALUES (?, ?, ?)", (name, address, phone))
conn.commit()
conn.close()
def delete_customer(name):
conn = sqlite3.connect('customers.db')
c = conn.cursor()
c.execute("DELETE FROM customers WHERE name=?", (name,))
conn.commit()
conn.close()
def update_customer(name, address, phone):
conn = sqlite3.connect('customers.db')
c = conn.cursor()
c.execute("UPDATE customers SET address = ?, phone = ? WHERE name = ?", (address, phone, name))
conn.commit()
conn.close()
def view_customers():
conn = sqlite3.connect('customers.db')
c = conn.cursor()
c.execute("SELECT * FROM customers")
customers = c.fetchall()
conn.close()
return customers
def search_customer(name, phone):
conn = sqlite3.connect('customers.db')
c = conn.cursor()
c.execute("SELECT * FROM customers WHERE name=? OR phone=?", (name, phone))
customers = c.fetchall()
conn.close()
return customers
def main():
st.title("Customer Database App")
create_database()
name = st.text_input("Name")
address = st.text_input("Address")
phone = st.text_input("Phone Number")
st.sidebar.header("Click for operations")
if st.sidebar.button("Add"):
add_customer(name, address, phone)
if st.sidebar.button("Delete"):
delete_customer(name)
if st.sidebar.button("Update"):
update_customer(name, address, phone)
if st.sidebar.button("Search"):
customers = search_customer(name, phone)
st.header("Customers File")
st.table(customers)
if st.sidebar.button("View"):
customers = view_customers()
st.header("Customers File")
st.table(customers)
if __name__ == '__main__':
main() |