[英]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.