Hyperion
Hyperion

Reputation: 2625

Python - Insert filename into database with sqlite3

I've created this table in my database:

cur.execute("""CREATE TABLE VIDEO(ID VARCHAR(128) NOT NULL PRIMARY KEY)""")

and what my script is supposed to do is to read all the files in a folder and put the filename into the VIDEO table (the filename is the ID and primary key):

def AddVideo(video):
    con = sqlite3.connect("C:\\Users\\Francesco\\Desktop\\db\\Database.db")
    with con:
        cur = con.cursor()
        cur.execute("""PRAGMA foreign_keys=ON""")
        cur.execute("""INSERT INTO VIDEO(ID) VALUES(?)""", (video))

But I got this error:

    cur.execute("""INSERT INTO VIDEO(ID) VALUES(?)""", (video))
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 26 supplied.

Now 26 is the the number of characters in the filename that I'm trying to add, all files are read from the folder with this:

for video in os.listdir(VIDEO_FOLDER):
  AddVideo(video)

Upvotes: 0

Views: 777

Answers (1)

Gil
Gil

Reputation: 380

If I am not mistaken, the cur.execute() function takes a tuple of parameters as its second argument. It's treating your value parameter as a tuple, which in this case is 26 characters long. To make it a tuple, just add a comma!

cur.execute("""INSERT INTO VIDEO(ID) VALUES(?)""", (video,))

Upvotes: 2

Related Questions