Python Forum

Full Version: Adding data to a table in SQLite3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to add data to a table in SQLite3 using the following code:

import sqlite3 # imports the SQLite library
from tkinter import * # imports tkinter library

with sqlite3.connect("TestScores.db") as db: # creates TestScores database
    cursor = db.cursor()

cursor.execute("""CREATE TABLE IF NOT EXISTS Scores(Name text PRIMARY KEY, 
Grade text NOT NULL);""") # creates the Scores table with Name and Grade as fields


def Add():
    Name = textbox1.get() # gets Name from textbox1
    Grade = textbox2.get() # gets Grade from textbox2
    cursor.execute("""INSERT INTO Scores(Name, Grade)
    VALUES("?","?")""", [Name],[Grade]) # adds this entry into the table
    db.commit() # this line saves the changes
I get this error:

Error:
Traceback (most recent call last): File "C:\Users\djwil\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "c:/Users/djwil/Documents/python/learning python/Chapter 18 - SQLite/Ch18-c7.py", line 14, in Add cursor.execute("""INSERT INTO Scores(Name, Grade) TypeError: function takes at most 2 arguments (3 given)
I'm not sure why I get this error as I only have 2 arguments being entered in a tkinter window.
you want
 cursor.execute("""INSERT INTO Scores(Name, Grade) VALUES(?,?)""", (Name,Grade))
cursor.execute takes 2 arguments - the sql statement and the values, as a tuple/list
Thanks :)