首页 > 解决方案 > 如何纠正 TypeError:execute() 需要 2 到 4 个位置参数,但给出了 5 个?

问题描述

我真的在这里挣扎..所以立即帮助将不胜感激。明天必须提交一个项目。如果这个问题被重复,请把答案的链接发给我。

if user_input==2:
    number=int(input("Enter number of tickets you want to book-"))
    for b in range(0,number):
        passengername=input("Enter name of passenger-")
        starting_station=input("Enter name of station from where passenger will board train-")
        destination=input("Enter name of station passenger wishes to reach-")
                    
        print("Ticket Booked!")
        mydb=mysql.connector.connect(host="localhost",user="root",passwd="",database="railways")
        mycursor=mydb.cursor()
        mycursor.execute("select train_no from route_fare where from_station=%s",(starting_station,), " and to_station=%s",(destination,))
        for w in mycursor:
            train_number_p=w
        

输出:

Enter number of tickets you want to book-1
Enter name of passenger-Ollie
Enter name of station from where passenger will board train-Jaipur Railway Station
Enter name of station passenger wishes to reach-Churu Railway Station
Ticket Booked!
Traceback (most recent call last):
  File "<pyshell#9>", line 14, in <module>
    mycursor.execute("select train_no from route_fare where from_station=%s",(starting_station,), " and to_station=%s",(destination,))
TypeError: execute() takes from 2 to 4 positional arguments but 5 were given

标签: pythonpython-3.xmysql-connector-python

解决方案


推荐阅读