Question: This is to be coded in Python Console Modify the code below and add the Add User function to it. Add User - create a
- This is to be coded in Python Console
- Modify the code below and add the Add User function to it.
- Add User - create a new function that asks for input for the following fields:
- first name
- last name
- email address
- phone number
- street address
- city
- state
- zipcode
- Be sure to update your user menu with the new Add User option.
Remove any reference to DROP the user table. We want our user table to persist and not be wiped clean every time we run the program.
You will need to make sure that you only create the user table once by writing your query like this:
CREATE TABLE IF NOT EXISTS user
Start with an empty user table. Remove the insert statements from Code Practice 9. Populate your table with 5 users using your Add User function that you are creating for this assignment.
Test that your user input exists in the database even after you stop and rerun the program. The data should be persistent.
You do not have to validate the user input for this activity. You can if you want to, but it is not required.
# import the sqlite3 database module
import sqlite3
# create a connection to the database file
conn = sqlite3.connect("myDatabase.db")
# create a cursor that we will use to move through the database
cursor = conn.cursor()
# Create the table if it doesn't exist
cursor.execute("CREATE TABLE IF NOT EXISTS user (first_name TEXT, last_name TEXT, email TEXT, phone_number TEXT, street_address TEXT, city TEXT, state TEXT, zipcode INTEGER);")
# Function of get user's first and last name
def FirstLastName():
# store the result of qurery into a list
Names = cursor.execute("SELECT first_name, last_name FROM user;")
#loop through the to print the result
for name in Names:
print(name[0], name[1])
# function to get user's name and content details
def NameandContact():
# store the result of qurery into a list
nameandcontact = cursor.execute("SELECT first_name, last_name, email, phone_number FROM user;")
#loop through the to print the result
for detail in nameandcontact:
print(detail[0], detail[1], detail[2], detail[3])
# function to get user's name and address
def NameandAddress():
# store the result of qurery into a list
nameaddress = cursor.execute("SELECT first_name, last_name, street_address, city, state, zipcode FROM user;")
#loop through the to print the result
for detail in nameaddress:
print(detail[0], detail[1], detail[2], detail[3], detail[4], detail[5])
# function to get all details from table
def All():
# store the result of qurery into a list
details = cursor.execute("SELECT first_name, last_name, email, phone_number, street_address, city, state, zipcode FROM user;")
#loop through the to print the result
for detail in details:
print(detail[0], detail[1], detail[2], detail[3], detail[4], detail[5], detail[6], detail[7])
if __name__=="__main__":
choice = 0
while(1):
print(""" Welcome, Enter your choice
1 - Show full table
2 - Show First Name's and Last Name's
3 - Show Contact and Name
4 - Get Name and Address
0 - Exit""")
choice = int(input())
if choice == 0:
break
if choice == 1:
All()
elif choice==2:
FirstLastName()
elif choice==3:
NameandContact()
elif choice==4:
NameandAddress()
else:
print("Invalid ! ")
# Save the update to the database
conn.commit()
# Close the connection
conn.close()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
