Link to home
Start Free TrialLog in
Avatar of Isaiah Melendez
Isaiah Melendez

asked on

Python 3.5.2 - I need help with a script

IDLE: Python 3.5.2
OS: Windows 10

I have a python app that accepts user input from an end-user and then stores the input into variables. Once all pieces of data are entered by the end-user an SQL statement runs to insert into an MSSQL database.

What I am trying to do is the following:

When the app fires up a statement reads "What would like to do?
        1) Insert a new Store CIM
        2) Update an existing store
        3) Delete an existing store
        4) Exit this application...

If the user selects 1, then it goes to the part of the script that does the process to complete 1, same for 2-4. I have completed #1 already and I am working on #2. I just need to understand and see the logic built to do the GUI if you will where the end-user will be able to select options when the app fires up.

Thank you for your help in advance.
Avatar of aikimark
aikimark
Flag of United States of America image

What does your Python code look like?
Avatar of Isaiah Melendez
Isaiah Melendez

ASKER

aikimark, I believe I figured it out. I used a while true loop that allows me to print statements with 4 options and each option accepts input from end user and compares it to a user_exit = 4 variable. If end user input is not 4 the loop continues.

Is there a better way?
Not without seeing the code.  I would think you should package the code into routines (DEF) so you aren't trying to maintain one big ball of code.
#libraries
import pyodbc
import sys
import datetime

#loop that will ask user for input and then do a function based on user

#create definition to display function later
def option_one():
    getstore_UIN = input('Enter the store UIN (ex: TX001):')
    print('')
    print('You entered %s' % getstore_UIN)
    print('')
    getstore_Num = input('Enter the store number (ex:001):')
    print('')
    print('You entered %s' % getstore_Num)
    print('')
    getstore_FileName = input('Enter the store MDB file name (ex: UIN.mdb):')
    print('')
    print('You entered %s' % getstore_FileName)
    print('')
    getstore_Name = input('Enter the store name:')
    print('')
    print('You entered %s' % getstore_Name)
    print('')
    getstore_CIM = input('Enter the store ROW CIM number:')
    print('')
    print('You entered %s' % getstore_CIM)
    print('')
    getstore_OpeningDate = input('Enter the store opening date (ex: mm/dd/yy): ')
    print('')
    print('You entered %s' % getstore_OpeningDate)
    print('')
    getstore_Email = input('Enter the store shop email (ex:):')
    print('')
    print('You entered %s' % getstore_Email)
    print('')
    getstore_Bays = input('Enter the number of bays this store has:')
    print('')
    print('You entered %s' % getstore_Bays)
    print('')
    getstore_Operator = input('Enter the full name of the store operator:')
    print('')
    print('You entered %s' % getstore_Operator)
    print('')
    getstore_Phone = input('Enter the store phone number:')
    print('')
    print('You entered %s' % getstore_Phone)
    print('')
    getstore_Market = input('Enter the name of the market the store is in (ex: Denver, Houston, etc):')
    print('')
    print('You entered %s' % getstore_CIM)
    print('')
    getfranchisee_Email = input('Enter the of the email of the franchisee (ex: first.lastname@cbauto.net:')
    print('')
    print('You entered %s' % getfranchisee_Email)
    print('')
    getstore_Street = input('Enter the store street address:')
    print('')
    print('You entered %s' % getstore_Street)
    print('')
    getstore_City = input('Enter the city the store is in:')
    print('')
    print('You entered %s' % getstore_City)
    print('')
    getstore_state = input('Enter the state the store is in:')
    print('')
    print('You entered %s' % getstore_state)
    print('')
    getstore_zip = input('Enter the zip the store is in:')
    print('')
    print('You entered %s' % getstore_zip)
    print('')

    #create the ODBC connection to MSSQL
    cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER={SQL2};DATABASE={ROWTest};UID={sa};PWD={pw}')

    #create the cursor variable to activate the connection
    cursor = cnxn.cursor()

    #sql command that will execute the insert to the database
    SQLCommand = ("INSERT INTO StoreInfo (store_UIN, store_Num, store_FileName, store_Name, store_CIM, store_OpeningDate, store_Email, store_Bays, store_Operator, store_Phone, store_Market, franchisee_Email, store_Street, store_City, store_State, store_Zip) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")

    #variables that grab user input variables and passes them to SQL command
    store_UIN = getstore_UIN
    store_Num = getstore_Num
    store_FileName = getstore_FileName
    store_Name = getstore_Name
    store_CIM = getstore_CIM
    store_OpeningDate = getstore_OpeningDate
    store_Email = getstore_Email
    store_Bays = getstore_Bays
    store_Operator = getstore_Operator
    store_Phone = getstore_Phone
    store_Market = getstore_Market
    franchisee_Email = getfranchisee_Email
    store_Street = getstore_Street
    store_City = getstore_City
    store_State = getstore_state
    store_Zip = getstore_zip

    #execute SQL statement
    cursor.execute(SQLCommand, [store_UIN, store_Num, store_FileName, store_Name, store_CIM, store_OpeningDate, store_Email, store_Bays, store_Operator, store_Phone, store_Market, franchisee_Email, store_Street, store_City, store_State, store_Zip])

    #commit the sql command
    cnxn.commit()

    input('Press enter to exit program')
    

#variable that makes the exit variable
user_exit =  4
while True:
        #prints statements that lead the end-user to decide what they would like to do
    print('Welcome to the ROWriter Database applet! What would you like to do?')
    print('')
    print('1) Insert a new store CIM info...')
    print('2) Update an existing store CIM info...')
    print('3) Delete an exisiting store CIM info...')
    print('4) Exit the application...')

        #accepts user input and converts it to a string and stores into a variable
    user_input = int(input('Enter option:'))
    #if user input is equal to the exit variable then the application exits
    if user_input == user_exit:
        break
    elif user_input == 1:
            option_one()
    elif user_input == 2:
            print('Hello, world!')
            

Open in new window

1. from a user interface perspective, I'd rather see a GUI
2. wait until you have gathered all of the data and then do a single presentation of the input data to the user along with any validation error messages.
3. you should look at your exposure to SQL injection attacks
https://xkcd.com/327/
We have a continuous running table. What this means is that we have 158 records (stores in existence) and option1 allows an end-user add more stores as time goes.

could you expand more on rather seeing a GUI?

That was a funny little comic strip. :) Thanks for sharing. FYI this will be open internally and not externally to end-users outside the IT department. I know it looks elementary and I will research hardening my SQL injection exposure.
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This pointed me in the direction I needed to go to get a GUI implemented.