簡體   English   中英

Python/MySQL TypeError:execute() 需要 2 到 4 個位置參數,但給出了 5 個

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

嘗試插入數據庫后發生錯誤。 執行后我收到以下回溯:

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

嘗試插入數據庫后,錯誤發生在執行時。 這是代碼:

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

謝謝你的幫助。

問題是cursor.execute的參數需要指定為一個元組,而不是單獨指定。

嘗試更換

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

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

你肯定沒有做對一些事情。 你的論點很多。 這是一個例子:

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 MySQLdb
import pandas as pd

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM