-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
109 lines (88 loc) · 2.91 KB
/
main.py
File metadata and controls
109 lines (88 loc) · 2.91 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from os import name
import sqlite3
from sqlite3.dbapi2 import Cursor
import sys
connection = sqlite3.connect("people.db")
cursor = connection.cursor()
try:
cursor.execute("CREATE TABLE people (name TEXT, age INTEGER, skills STRING)")
except Exception as e:
pass
def user_is_unique(name):
rows = cursor.execute("SELECT name, age, skills FROM people").fetchall()
for user in rows:
if user[0] == name:
return False
return True
def insert_db():
name = input("Name >>")
if user_is_unique(str(name)):
age = input("Age >>")
skills = input("Skills >>")
if name !="" and age != "" and skills != "":
cursor.execute(f"INSERT INTO people VALUES ('{name}', '{age}', '{skills}')")
connection.commit()
print(name + " has been added to the database!")
else:
print("One of the fields are empty!")
insert_db()
else:
print("Name is already in the database!")
def edit_db():
name = input("Type name of the person you would like to edit >>")
field = input("Which field would you like to edit: name, age, or skills? >>")
updated_field = input ("What would you like to update it to? >>")
try:
cursor.execute(f"UPDATE people SET {field} = ? WHERE name = ?", (updated_field, name))
connection.commit()
print("Successfully updated user!")
except Exception as e:
print(e)
def get_user_info_db():
target_name = input("Who do you wnat to see information about? >>")
rows = cursor.execute("SELECT name,age,skills FROM people WHERE name = ?", (target_name,),).fetchall()
name = rows[0][0]
age = rows [0][1] # rows [(name, age, skills)]
skills = rows [0][2]
print (f"{name} is {age} years old, and works as a {skills}.")
def delete_db():
name = input ("Type the name of the person that you would like to delete >>")
if name != "":
cursor.execute("DELETE FROM people WHERE name = ?", (name,))
connection.commit()
print("User successfully deleted!")
def display_db():
rows = cursor.execute("SELECT name, age, skills FROM people ORDER BY name ASC").fetchall()
print("Users: ")
for user in rows:
print(f"- {user[0]} - {user[1]} - {user[2]}")
def exit_db():
cursor.close()
connection.close()
sys.exit()
def select_options():
options = input("""
----------------------
Type '0' to exit
Type '1' to insert a new user
Type '2' to display user
Type '3' to delete user
Type '4' to edit user
Type '5' to get user information
----------------------
>> """)
if options == "0":
exit_db()
if options == "1":
insert_db()
if options == "2":
display_db()
if options == "3":
delete_db()
if options == "4":
edit_db()
if options == "5":
get_user_info_db()
# Infinite loop
while True:
select_options()