【问题标题】:TypeError: function takes at most 2 arguments (4 given)TypeError:函数最多接受 2 个参数(给定 4 个)
【发布时间】:2021-11-22 19:36:33
【问题描述】:

我使用 sqlite3 数据库。最后一行出现错误 - cursor.execute("INSERT INTO player VALUES(?,?,?);", playe, leve, balance) TypeError:函数最多接受 2 个参数(给定 4 个)

import sqlite3

connect = sqlite3.connect("Stats.db")
cursor = connect.cursor()

cursor.execute("""CREATE TABLE IF NOT EXISTS player(
    name TEXT,
    cash INT,
    level INT
)""")
connect.commit()

playe = input("Name: ")
balance = 10
leve = 0
cursor.execute("INSERT INTO player VALUES(?,?,?);", playe, leve, balance)

【问题讨论】:

  • 你需要把你的参数打包成一个元组:(playe, leve, balance)

标签: python sqlite


【解决方案1】:

使用元组

cursor.execute("INSERT INTO player VALUES(?,?,?);", (playe, leve, balance))

我看到你写的数据没有插入,你可以这样做解决这个问题:

connect.commit()
connect.close()

【讨论】:

    【解决方案2】:

    使用cursor.execute 时,您需要将值作为元组或列表(或任何迭代器)传递。

    您只需将playe, leve, balance 替换为(playe, leve, balance) 即可。

    整行:

    cursor.execute("INSERT INTO player VALUES(?,?,?);", (playe, leve, balance))
    

    【讨论】:

      猜你喜欢
      • 2016-02-03
      • 2019-01-20
      • 2017-06-30
      • 2019-09-12
      • 2013-01-13
      • 2018-12-23
      • 1970-01-01
      相关资源
      最近更新 更多