5

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.

3 Answers 3

26

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))
Sign up to request clarification or add additional context in comments.

Comments

2

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,))

Comments

1

Import required Packages

import MySQLdb
import pandas as pd

MySQLdb is an interface to the popular MySQL database server for Python.

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.