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 | |
+-----------+--------------+------+-----+---------+----------------+
"". If any of them is an empty string, do not push the updates.