Python Forum
Small Database Program (Need Help) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Small Database Program (Need Help) (/thread-28245.html)



Small Database Program (Need Help) - thewetmosquito - Jul-11-2020

Hello Python community,

I'm new around these parts, and I've picked up on Python last week to begin understanding and writing working programs. I have been referencing the book "Automate the Boring Stuff" while I learn to program, but decided to pause at Chap4 to write some programs of my own.(taking off the training wheels).

The program I'm trying to write is a simple database that asks for your First Name, Last Name, email address, and birthday. Once all information is acquired, it is stored inside a database list. Once the program ends, it prints the database list(just to know it works without recalling the database).
Result: ['John Doe', '[email protected]', 'Jan 1 1996']

I have succeeded in doing this part, but now I want to carry out this process 3 times. Each time the cycle is carried out, I want a new row/column to be appended inside the database list.
Intended Result: [['John Doe', '[email protected]', 'January 1 1996'],['Johnny Appleseed', '[email protected]', 'January 2 1996']]

I have gone online to look for answers to this problem, and came across some possible solutions, but they are obscure to me; thus hard for me to apply to the code that I had already written:

import time, sys


database = []
firstName = ''
lastName = ''
email = ''
name = []
birthday = []
months = ['January','February','March','April','May','June','July',
         'August','September','October','November','December']

print('Database')
print('\n')


# first name 
while True: 
    print('Please enter your first name. ' , end='')
    print('(Cannot include special characters)')
    firstName = input()
    if firstName.isdigit(): # validates characters
        print('Invalid character found')
        time.sleep(1.0) # 1 second pause before re-prompting name
    else:
        name.append(firstName.upper())
        break
# last name
while True:
    print('Please enter your last name. ' , end='')
    print('(Cannot include special characters)')
    lastName = input()
    if lastName.isdigit(): # validates characters
        print('Invalid character found')
        time.sleep(1.0) # 1 second pause before re-prompting name
    else:
        name.append(lastName.upper())
        break

# Concatenates first and last name strings into
# One string. Appends the 'name' variable to database
# as database[0]

name = name[0] + ' ' + name[1]
database.append(name)

# email
while True:
    # validates email
    input_email = input('What is your email address? ')
    if '@' not in input_email:
        print("Make sure to include the '@' in your email address. " , end='')
        print("Please try again")
        time.sleep(1.0)
    else:
        database.append(input_email)
        break

# birthday
while True:
    # validates birth month
    input_month = input('What is your month of birth? ')
    if input_month not in months:
        print('Invalid month. Please try again.')
        time.sleep(1.0)
    else:
        birthday.append(input_month)
        break

while True:
    # validates birth day
    try: 
        input_day = int(input('On what date were you born? '))
        if input_day >= 32:
            print('Invalid date. Please try again.')
            time.sleep(1.0)
        else:
            birthday.append(input_day)
            break
    except ValueError: # makes sure that string is not passed as an input
        print('Invalid date. Please try again.')
        time.sleep(1.0)
        input_day = int(input('On what date were you born? '))
    
while True:
    # validates birth year
    try: 
        input_year = int(input('What year were you born? '))
        if input_day >= 2021:
            print('This year has not happened yet. Please try again.')
            time.sleep(1.0)
        else:
            birthday.append(input_year)
            break
    except ValueError: # makes sure that string is not passed as an input
        print('Invalid entry. Please try again.')
        time.sleep(1.0)
        input_day = int(input('What year were you born? '))    

# concatenates 'birthday' list
# & appends them to database as
# database[2]

birthday = birthday[0] + ' ' + str(birthday[1]) + ' ' + str(birthday[2])
database.append(birthday)

print(database)
What can I add to my pre-existing code that won't break the program when ran?


Side note: I feel like I wrote more lines than I needed to to carry the process out. If I did, my apologies. I was really focussed on making the program make logical sense on the end-user side. Smile


RE: Small Database Program (Need Help) - Larz60+ - Jul-11-2020

A suggestion to make code easier to maintain, understand and expand, would be to use a DBMS
simple one, like sqlite3 and SQLalchemy to make access to data even simpler.

I have a tutorial on sqlalchemy here: https://python-forum.io/Thread-SqlAlchemy-Tutorial-Basic-Model-database-Creation-and-Data-Load


RE: Small Database Program (Need Help) - GOTO10 - Jul-11-2020

(Jul-11-2020, 03:06 AM)thewetmosquito Wrote: I want to carry out this process 3 times.
...
What can I add to my pre-existing code that won't break the program when ran?

There isn't a way for you to carry out the process 3 times without making any other changes to your code, but only a few changes would be needed. The simplest thing you could do would be to use a for loop like for _ in range(3) to loop through your code that gets user input and adds it to your database list.

If you add a for loop that runs lines 17 - 105, AND move the initialization of all your variables except database and months inside the loop, that will *almost* get you what you're looking for. Your database will end up being just one long list, instead of the list of lists you want, so you'll need to add a little additional code to handle that differently to get your desired output.


RE: Small Database Program (Need Help) - menator01 - Jul-11-2020

There are many ways to do this. I would suggest trying sqlite3. You can do multiple inserts at once. Just need to create a list of what you want inserted. Have a look at this link.