首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python检查来自用户输入的空值,并且不包括在sql更新中

Python检查来自用户输入的空值,并且不包括在sql更新中
EN

Stack Overflow用户
提问于 2018-08-03 08:54:00
回答 2查看 571关注 0票数 0

在我的函数中,我要求用户输入不同的变量,然后更新数据库。问题是,如果他们没有输入任何东西,有没有一种方法我不能将任何东西推送到DB?当他们没有为var输入值时,DB中的值将被清除。

代码语言:javascript
复制
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    |                |
    +-----------+--------------+------+-----+---------+----------------+
EN

回答 2

Stack Overflow用户

发布于 2018-08-03 09:21:14

您可以使用以下helper函数:

代码语言:javascript
复制
def mandatory_input(prompt):
    while True:
        value = input(prompt)
        if value:
            return value
        print("You did not enter the required information.")

并用它替换所有被认为是强制性的input提示,因此:

代码语言:javascript
复制
student_id = input("Enter the id of the student record you wish to modify: ")

变成:

代码语言:javascript
复制
student_id = mandatory_input("Enter the id of the student record you wish to modify: ")

依此类推,因此用户将一直被提示输入值,直到他们输入某些值。

票数 1
EN

Stack Overflow用户

发布于 2018-08-03 09:24:06

此外,如果您想等待用户完成,然后在用户输入后进行验证:

代码语言:javascript
复制
if all([firstname, lastname, email, ...]):
    cur = conn.cursor()
    ...
else:
    print('User missed an input')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51663957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档