Python 如何更正TypeError:execute()接受2到4个位置参数,但给出了5个?

Python 如何更正TypeError:execute()接受2到4个位置参数,但给出了5个?,python,python-3.x,mysql-connector-python,Python,Python 3.x,Mysql Connector Python,我在这里真的很挣扎。。因此,立即提供帮助将不胜感激。明天必须提交一个项目。如果这个问题被重复,请发送给我答案的链接 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-")

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

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
输入要预订的票数-1
输入乘客奥利的姓名
输入乘客将在斋浦尔火车站上车的车站名称
输入希望到达楚茹火车站的乘客姓名
订票了!
回溯(最近一次呼叫最后一次):
文件“”,第14行,在
mycursor.execute(“从路线中选择列车号,其中从站=%s”,(起点站),“到站=%s”,(终点站),)
TypeError:execute()接受2到4个位置参数,但给出了5个

未经测试,但请尝试
mycursor.execute(“从路线中选择列车号,其中从车站=%s到车站=%s)”,(起点站,终点站))
此错误消息对于初学者来说非常混乱
mycursor
是类的实例。当像
instance.method()
或在您的例子中是
mycursor.execute()
那样调用时,任何实例都会隐式地将实例作为第一个参数。因此,实际上您正在处理
execute()
5个参数。正如@JustinEzequiel所指出的,你的第二个参数是元组的原因是,你可以给它多个参数。我找不到更好的文档,但先看看你哪里出错了。谢谢!上面的代码修复了它!未经测试,但请尝试
mycursor.execute(“从路线中选择列车号,其中从\u站=%s到\u站=%s)”,(起点站,终点站))
此错误消息对于初学者来说非常混乱
mycursor
是类的实例。当像
instance.method()
或在您的例子中是
mycursor.execute()
那样调用时,任何实例都会隐式地将实例作为第一个参数。因此,实际上您正在处理
execute()
5个参数。正如@JustinEzequiel所指出的,你的第二个参数是元组的原因是,你可以给它多个参数。我找不到更好的文档,但先看看你哪里出错了。谢谢!上面的代码修复了它!