parameters错误:valueerror:'params'arg(< class'list'>)只能是元组或字典

p5fdfcr1  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(728)

我正在尝试向数据库中插入一些不一致的bot数据。我做了参数法,但它不工作。我对用python处理mysql还不熟悉。
我的代码

@bot.event

# Edit Log

async def on_message_edit(before, after):
    MemberId = before.author.id
    await bot.send_message(bot.get_channel('480526097331650590'), 'The user <@%s> have edited his message from ``' % (MemberId) + before.content + '`` to `` ' + after.content + ' `` ')

    #Connection to sql
    conn = pymssql.connect(server='HAMOOOOD25\DISCORDPY', user='sa', password='31045', database='ChatLogs')
    cursor = conn.cursor()

    #Declaring vars
    BeforeId = before.id
    BAuthId = before.author.id
    BAuthN = before.author.id
    Befcon = before.content
    Aftcon = after.content

    sql = "INSERT INTO Editedmsg VALUES (Message_ID, Message_Author_ID, Message_Owner_Name, Previous_Message, After_Message)"
    cursor.execute(sql, [(before.id), (before.author.id,), (before.author.id), (before.content), (after.content)])
    conn.close()

我的错误

ValueError: 'params' arg (<class 'list'>) can be only a tuple or a dictionary.
z9ju0rcb

z9ju0rcb1#

显然,错误消息建议您使用 tuple 而不是 list 在你的 cursor.execute 打电话。除此之外,你的 sql 查询的格式不正确(请参阅示例部分)。你的 execute 电话应该是这样的:

sql = "INSERT INTO Editedmsg VALUES (%d, %d, %d, %s, %s)"
params = (before.id, before.author.id, before.author.id, 
          before.content, after.content)
cursor.execute(sql, params)

相关问题