0

i get TypeError: function takes exactly 2 arguments (3 given), when i convert show_id and genre into list of tuples, i get ValueError: paramenter are of unsupported type.

import sqlite3
import csv

# create database 
conn = sqlite3.connect("favorites8.db")

open("favorites8.db", "w")
db = conn.cursor()

# create table
db.execute("CREATE TABLE shows (id INTEGER AUTO INCREMENT, title TEXT NOT NULL, PRIMARY KEY (id))")

db.execute("CREATE TABLE genres (shows_id INTEGER, genre TEXT NOT NULL, FOREIGN KEY (shows_id) REFERENCES shows(id))")

# open csv file 
with open("favorites.csv", "r") as file:
    # create dictreader
    reader = csv.DictReader(file)
    # iterate over csv file
    for row in reader:
        # canonicalize title
        title = row["title"].strip().upper()
        # insert title
        stmnt1 = "INSERT INTO shows (title) VALUES(?)"
        show_id = db.execute(stmnt1, (title,))
        
        # Insert genres
        for genre in row["genres"].split(", "):
            stmnt2 = "INSERT INTO genres (shows_id, genre) VALUES(?, ?)"
            
            db.executemany(stmnt2, show_id, genre)
    # commit changes
    conn.commit()
    conn.close()
# end with
   
4
  • Can you please share the content of favorites.csv? Commented Aug 7, 2022 at 12:24
  • Timestamp,title,genres 10/28/2019 15:03:45,Dynasty,"Drama, Family" 10/28/2019 15:04:48,The Office,Comedy 10/28/2019 15:09:00,Blindspot,"Action, Crime, Drama" 10/28/2019 15:09:02,24,Action 10/28/2019 15:09:02,Friends,Comedy 10/28/2019 15:09:05,psych,Comedy 10/28/2019 15:09:09,Veep,Comedy 10/28/2019 15:09:09,Survivor,Reality-TV ..... Commented Aug 7, 2022 at 13:17
  • db.executemany(stmnt2, show_id, genre) should be db.execute(stmnt2, (show_id, genre)). executemany is for when you want to insert / update / delete multiple sets of parameters in a single statement. Commented Aug 7, 2022 at 15:28
  • This also generates sqlite3.InterfaceError: error binding parameter 0 - probably unsupported type. Commented Aug 7, 2022 at 18:33

1 Answer 1

1

I've fixed some mistakes and cleaned this up a bit:

conn = sqlite3.connect("favorites8.db")

db = conn.cursor()

db.execute("CREATE TABLE shows (id INTEGER NOT NULL PRIMARY KEY, title TEXT NOT NULL)")

db.execute("CREATE TABLE genres (shows_id INTEGER, genre TEXT NOT NULL, FOREIGN KEY (shows_id) REFERENCES shows(id))")

with open("favorites.csv", "r") as file:
    reader = csv.DictReader(file)
    for row in reader:
        title = row["title"].strip().upper()

        stmnt1 = "INSERT INTO shows (title) VALUES(?)"
        db.execute(stmnt1, (title,))
        show_id = db.lastrowid

        # Insert genres
        data = []
        stmnt2 = "INSERT INTO genres (shows_id, genre) VALUES(?, ?)"
        for genre in row["genres"].split(", "):
            data.append((show_id, genre))

        db.executemany(stmnt2, data)
    # commit changes
    conn.commit()
    conn.close()

There were a bunch of issues:

  1. executemany accepts iterable as a second argument.
  2. First execute statement does not return id, but cursor object, you need to retrieve it manually.
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this works as expected, though i have to comment the first conn.commit(). Thanks again.
And show id is null.
@AbuAminu change to CREATE TABLE shows (id INTEGER NOT NULL PRIMARY KEY, title TEXT NOT NULL) this is the correct way

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.