简体   繁体   English

Python SQLite用户查询未通过验证

[英]Python SQLite User query not validating

I am writing a python function that takes an input from a user, searches for it in a database and then checks the input against the return result and if correct proceeds with the program and if incorrect asks the user to re-enter. 我正在编写一个python函数,该函数从用户处获取输入,在数据库中搜索该输入,然后根据返回结果检查该输入,是否正确执行了该程序,以及是否不正确要求用户重新输入。 This doesn't work however. 但是,这不起作用。 When a user enters something it just says it is incorrect as per my program. 当用户输入某些内容时,它只是说按照我的程序是不正确的。 Here is the code: 这是代码:

def TakeOutItem():
Serial = input("Please enter the serial number of the music you are taking out: ")
SerialN = int(Serial,)
with sqlite3.connect("school.db") as db:
    cursor = db.cursor()
    cursor.execute("SELECT SerialNo FROM Music WHERE SerialNo=?",(SerialN,))
    data = cursor.fetchone()[0]
    print(">>",SerialN, data,">>")
    print(type(SerialN),type(data)).show
    print(int(SerialN) == int(data))
    if SerialN == int(data):
        DateOut = date.today()
        DateIn = date.today() + timedelta(days=30)
        cursor.execute('SELECT Name FROM Music WHERE SerialNo=?', Serial)
        NameofMusic = cursor.fetchone()
        cursor.execute('SELECT StudentID FROM Student WHERE username=?', student_login(username))
        StudentID = cursor.fetchone()
        cursor.execute('INSERT INTO MusicLoan VALUES (?,?,?,?,?)', DateOut, DateIn, Music, NameofMusic, StudentID)
        print("Item taken out successfully")
        student_menu()

    else:
        print("Incorrect Serial Number please try again")
        TakeOutItem()

and this is the error screen 这是错误屏幕

What would you like to do?

1. Take out item

2. Return item

3. Exit

Please select an option number: 1

Please enter the serial number of the music you are taking out: 1
>> 1 1 >>
<class 'int'> <class 'int'>
Please select a valid option

The it takes me back to he previous menu 它带我回到他以前的菜单

How can I fix this error?? 如何解决此错误?

cursor.fetchone() will always return a tuple .So use cursor.fetchone()将始终返回一个tuple 。因此请使用

data = cursor.fetchone()[0]

or 要么

if SerialN == data[0]:

Inside your if condition also, please rewrite it to NameofMusic[0] and StudentID[0] in your INSERT query 同样在您的if条件中,请在您的INSERT查询NameofMusic[0]其重写为NameofMusic[0]StudentID[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM