简体   繁体   English

Python/MySQL TypeError:execute() 需要 2 到 4 个位置参数,但给出了 5 个

[英]Python/MySQL TypeError: execute() takes from 2 to 4 positional arguments but 5 were given

The error happens after trying to insert into the database.尝试插入数据库后发生错误。 I recive the following traceback after executing:执行后我收到以下回溯:

Traceback (most recent call last):
File "C:\Python33\Archive\MySQL-teste12.py", line 278, in <module>
inserir(cursor, cx2)
File "C:\Python33\Archive\MySQL-teste12.py", line 196, in inserir
cursor.execute(add_produto, va, input_date, vc)
TypeError: execute() takes from 2 to 4 positional arguments but 5 were given

The error happens on the execute, after trying to insert into the database.尝试插入数据库后,错误发生在执行时。 Here is the code:这是代码:

def inserir (cursor, db):
menu3 = 0
while menu3 != 99:
    print("""
----- Menu Banco MARK II, v.1.00, MySQL, VR -----

          ----- Menu de Inserção ----


1.Inserir em produto.
2.Inserir em cliente.
3.Inserir em empregado.
4.Inserir em salario.
99.Sair.

    """)
    menu3 = input("Digite sua Opção")

    if menu3 == '1':
        va = input("""

                   Digite o Nome do Produto.

                   """)

        vb = input("""

                   Digite a data de Lançamento do Produto (Ano/mês/dia).

                   """)
        input_date = datetime.strptime(vb, '%Y/%m/%d')

        vc = input("""

                   Digite o Preço do Produto (ex: 20, 20.33).

                   """)

        add_produto = """INSERT INTO produto(nome,
              data_lcm, preco)
              VALUES (%s, %s, %s)"""

        #try:
        cursor.execute(add_produto, va, input_date, vc)
        db.commit()
        print("""
              Inserção concluida com sucesso.

              """)
        #except:
         #   db.rollback()
          #  print("""

           #     Erro.

            #    """)
    if menu3 == '99':
        break

Thanks for any help.谢谢你的帮助。

The problem is that the arguments to the cursor.execute need to be specified as one tuple, not individually.问题是cursor.execute的参数需要指定为一个元组,而不是单独指定。

Try replacing尝试更换

        cursor.execute(add_produto, va, input_date, vc)

with

        cursor.execute(add_produto, (va, input_date, vc))

You're definately not getting some things right.你肯定没有做对一些事情。 Your argument list is much.你的论点很多。 This is an example:这是一个例子:

import MySQLdb
db=MySQLdb.connect(passwd="root",db="playful")

cursor=db.cursor()
use = 'python'
cursor.execute("SELECT the_error FROM coding WHERE tag < %s", (use,))

Import required Packages导入所需的包

import MySQLdb
import pandas as pd

MySQLdb is an interface to the popular MySQL database server for Python. MySQLdb 是用于 Python 的流行 MySQL 数据库服务器的接口。

myDataBase = MySQLdb.connect(host     = "localhost",
                             user     = "root"     ,
                             password = "*******"  ,
                             db       = "ABCD"     )

myCursor = myDataBase.cursor()
sql = "INSERT INTO ABCD.xyz(A,B,C,D, E, F, G, H) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"

# create timer
start_time = time.time()
for index, rowid in LOBs.iterrows():
    # print(tuple(rowid))
    myCursor.execute(sql, tuple(rowid))
myDataBase.commit()
myCursor.close()
myDataBase.close()

# see total time to do insert
print("%s seconds ---" % (time.time() - start_time))

暂无
暂无

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

相关问题 类型错误:execute() 需要 2 到 3 个位置参数,但给出了 7 个 - TypeError: execute() takes from 2 to 3 positional arguments but 7 were given Python TypeError:接受 4 个位置参数,但给出了 5 个 - Python TypeError: takes 4 positional arguments but 5 were given TypeError: with_column() 从 3 到 4 个位置 arguments 但给出了 5 个(python) - TypeError: with_column() takes from 3 to 4 positional arguments but 5 were given (python) Python: TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given - Python : TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given Python TypeError:衍生物_circ()接受2个位置参数,但给出了6个 - Python TypeError: derivatives_circ() takes 2 positional arguments but 6 were given TypeError:__init __()接受2个位置参数,但是给了3个Python 3? - TypeError: __init__() takes 2 positional arguments but 3 were given Python 3? 类型错误:update() 需要 2 个位置参数,但给出了 3 个:Python - TypeError: update() takes 2 positional arguments but 3 were given : Python Python - TypeError: generateID() 需要 3 个位置 arguments 但给出了 4 个 - Python - TypeError: generateID() takes 3 positional arguments but 4 were given Python - 类型错误:__init__() 需要 3 个位置 arguments 但给出了 4 个 - Python - TypeError: __init__() takes 3 positional arguments but 4 were given Python - 类型错误:function 占用 3 个位置 arguments 但给出了 4 个 - Python - TypeError: function takes 3 positional arguments but 4 were given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM