Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

mysql

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.

like image 882
Rex Avatar asked Nov 28 '22 15:11

Rex


2 Answers

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))
like image 184
Luke Woodward Avatar answered Dec 05 '22 13:12

Luke Woodward


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,))
like image 32
iChux Avatar answered Dec 05 '22 14:12

iChux