Python Forum
Adding data to a table in SQLite3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding data to a table in SQLite3
#1
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.
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using pyodbc&pandas to load a Table data to df tester_V 3 799 Sep-09-2023, 08:55 PM
Last Post: tester_V
Question Using SQLAlchemy, prevent SQLite3 table update by multiple program instances Calab 3 745 Aug-09-2023, 05:51 PM
Last Post: Calab
  Rows not adding to sqlite3 database using SQLAlchemy Calab 11 1,653 Jun-02-2023, 05:53 PM
Last Post: bowlofred
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 923 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  Basic SQL query using Py: Inserting or querying sqlite3 database not returning data marlonbown 3 1,359 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  Django: Adding Row Data To Existing Model Instance Question/Problem. Steven_Pinkerton 1 1,244 Aug-09-2022, 10:46 AM
Last Post: Addweb
  panda table data kucingkembar 0 1,117 Mar-01-2022, 10:38 PM
Last Post: kucingkembar
  Sorting table data Blacktime2 1 1,323 Feb-26-2022, 07:05 PM
Last Post: ibreeden
  Adding shifted data set to data set xquad 3 1,495 Dec-22-2021, 10:20 AM
Last Post: Larz60+
  Strategy on updating edits back to data table and object variables hammer 0 1,194 Dec-11-2021, 02:58 PM
Last Post: hammer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020