不断收到错误TypeError:函数最多接受2个参数(给定3个) [英] Keep getting the error TypeError: function takes at most 2 arguments (3 given)

查看:55
本文介绍了不断收到错误TypeError:函数最多接受2个参数(给定3个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在上大学的课程项目,该项目涉及一个测验,该测验将所有背景数据存储在数据库中.通过添加外键,我试图找到一种方法来合并两个表之间的外键数据.例如,users表存储用户数据及其用户ID.数据表将存储有关测验级别等信息以及该特定用户ID.插入数据时,如何从两个表中自动更新该信息?

I am currently working on my coursework project for college which involves a quiz which stores all background data in a database. With the addition of foreign keys, i have tried to find a way to merge the data from the foreign key between two tables. For example , The users table stores user data and their UserID. The data table will store information about the level of the quiz,etc along with that specific user id. How would that information be automatically updated from the two tables when inserting data?

该方法似乎不起作用,下面是一些代码.

The method doesnt seem to work, here is some of the code.

difficulty= (1,)
users_id = (1,)  

#values of variables

def users_level(db,cursor,difficulty,users_id):

   cursor.execute("insert into Data (Level,UsersID) VALUES (?,?)",(difficulty),(users_id))

   db.commit()

然后出现错误:

   cursor.execute("insert into Data (Level,UsersID) VALUES (?,?)",(difficulty),(check_id))
TypeError: function takes at most 2 arguments (3 given)

是否有解决此问题的方法?或者可能是一种更容易/更有效的方法,可以使用外键从其他表中自动增加ID/数据.谢谢.

Is there a solution to this problem? Or potentially an even easier/more efficient method to auto increment id's/data from other tables with foreign keys. Thanks.

推荐答案

cursor.execute 接受2个参数(查询和查询args元组),但您要传递3个参数: cursor.execute(插入数据(Level,UsersID)VALUES(?,?)",(困难),(users_id))

cursor.execute takes 2 arguments (the query and the query args tuple), yet you are passing it 3 arguments: cursor.execute("insert into Data (Level, UsersID) VALUES (?,?)",(difficulty),(users_id))

您应将(difficulty),(users_id)更改为2个元素的元组(difficulty,users_id):

You should change (difficulty),(users_id) to a 2-elements tuple, (difficulty, users_id):

cursor.execute("insert into Data (Level, UsersID) VALUES (?,?)", (difficulty, users_id))

这篇关于不断收到错误TypeError:函数最多接受2个参数(给定3个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆