问题
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()
I am then presented with error:
cursor.execute("insert into Data (Level,UsersID) VALUES (?,?)",(difficulty),(check_id))
TypeError: function takes at most 2 arguments (3 given)
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.
回答1:
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))
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))
来源:https://stackoverflow.com/questions/40465681/keep-getting-the-error-typeerror-function-takes-at-most-2-arguments-3-given