第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Python從用戶輸入中檢查NULL值,并且不包含在sql更新中

Python從用戶輸入中檢查NULL值,并且不包含在sql更新中

白板的微信 2021-05-06 18:31:53
在我的函數(shù)中,我要求用戶輸入不同的變量,然后更新數(shù)據(jù)庫(kù)。問題是,如果他們不輸入任何內(nèi)容,有沒有辦法我不能將任何內(nèi)容推送到數(shù)據(jù)庫(kù)?當(dāng)他們不輸入var的值時(shí),DB中的值將被清除。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")
查看完整描述

2 回答

?
倚天杖

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

您可以使用以下幫助器功能:


def mandatory_input(prompt):

    while True:

        value = input(prompt)

        if value:

            return value

        print("You did not enter the required information.")

并input用它替換所有被認(rèn)為是強(qiáng)制性的提示,因此:


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

變成:


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

依此類推,以便在用戶輸入內(nèi)容之前一直提示用戶輸入值。


查看完整回答
反對(duì) 回復(fù) 2021-05-18
  • 2 回答
  • 0 關(guān)注
  • 230 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)