Python/MySQL类型错误:execute()取2到4个位置参数,但给出了5个

时间:2021-12-04 04:44:01

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.

感谢任何帮助。

2 个解决方案

#1


5  

The problem is that the arguments to the cursor.execute need to be specified as one tuple, not individually.

问题在于游标的参数。执行需要指定为一个元组,而不是单独的。

Try replacing

尝试更换

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

with

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

#2


1  

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'
c.execute("""SELECT the_error FROM coding WHERE tag < %s""", (use,))

``

#1


5  

The problem is that the arguments to the cursor.execute need to be specified as one tuple, not individually.

问题在于游标的参数。执行需要指定为一个元组,而不是单独的。

Try replacing

尝试更换

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

with

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

#2


1  

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'
c.execute("""SELECT the_error FROM coding WHERE tag < %s""", (use,))

``