0

In my function I am asking for input from the user for different variables and then updating the DB. The issue is if they do not input anything is there a way I can not push anything to the DB? At the moment when they do not enter a value for the var, the value in the DB will get wiped out.

db_root = '/var/lib/mysql/'

db_to_create = 'students'
db_to_use = 'students'

conn = pymysql.connect(host='localhost',  user='root', passwd='dbadmin',  cursorclass=pymysql.cursors.DictCursor)

print('Connection successful!!')


def modify_student():
    student_id = input("Enter the id of the student record you wish to modify: ")
    student_info = input("Is this student personal information you want to modify - y or n: ")
    if student_info == 'y':
        firstname = input("Enter the first name: ")
        lastname = input("Enter the last name: ")
        email = input("Enter the email address: ")
        address = input("Enter the address: ")
        DOB = input("Enter the DOB in YYYY-MM-DD: ")

        cur = conn.cursor()
        command = "use %s; " %db_to_use
        cur.execute(command)

        sql = "UPDATE students_info SET firstname = %s, lastname = %s,  email = %s,  address = %s,  DOB = %s  WHERE ID = %s;"
        cur.execute(sql, [firstname, lastname, email, address, DOB, student_id])

        print(cur.execute)
        conn.commit()
        cur.close()
        conn.close()
    else:
        print("else")

modify_student()

######## SCHEMA ##################

        MariaDB [students]> DESCRIBE students_info;
    +-----------+--------------+------+-----+---------+----------------+
    | Field     | Type         | Null | Key | Default | Extra          |
    +-----------+--------------+------+-----+---------+----------------+
    | ID        | int(11)      | NO   | PRI | NULL    | auto_increment |
    | firstname | varchar(255) | YES  |     | NULL    |                |
    | lastname  | varchar(255) | YES  |     | NULL    |                |
    | email     | varchar(255) | YES  |     | NULL    |                |
    | address   | varchar(255) | YES  |     | NULL    |                |
    | DOB       | date         | YES  |     | NULL    |                |
    | english   | varchar(255) | YES  |     | NULL    |                |
    | maths     | varchar(255) | YES  |     | NULL    |                |
    | history   | varchar(255) | YES  |     | NULL    |                |
    | science   | varchar(255) | YES  |     | NULL    |                |
    +-----------+--------------+------+-----+---------+----------------+
1
  • Check each input value for not being an empty string "". If any of them is an empty string, do not push the updates. Commented Aug 3, 2018 at 0:56

2 Answers 2

1

You can use the following helper function:

def mandatory_input(prompt):
    while True:
        value = input(prompt)
        if value:
            return value
        print("You did not enter the required information.")

And replace all the input prompts that are deemed mandatory with it, so:

student_id = input("Enter the id of the student record you wish to modify: ")

becomes:

student_id = mandatory_input("Enter the id of the student record you wish to modify: ")

and so on, so that the user will be kept being prompted for a value until they enter something.

Sign up to request clarification or add additional context in comments.

Comments

1

In addition, if you want to wait for the user to finish, and then validate when they've entered at the end:

if all([firstname, lastname, email, ...]):
    cur = conn.cursor()
    ...
else:
    print('User missed an input')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.