Home > OS >  sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 3, and t
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 3, and t

Time:11-15

import sqlite3

database = r"files/users.db"
textfile = r"files/AlphaVList.txt"


class List:

    def getusers(f):
        return f


c = sqlite3.connect(database)

with open(textfile, "r") as openfile:
    file = openfile.read()
    # print(file)


nameList = []
addressList = []

fullAddress = file.split("\n")
fullAddress = fullAddress[1:]

userList = file.split('@')

try:
    for i in userList:
        u = i.split("\n")
        nameList.append(u[1])
        addressList.append(u[0])
except IndexError:
    pass
addressList.remove("")

c.executemany(f"INSERT INTO users (fullAddress, name, mailAddress) VALUES (?, ?, ?)", (tuple(fullAddress), tuple(nameList), tuple(addressList),))

Why am I getting this error?

The code was supposed to upload 3 lists into sql, and when I tried to insert the lists I got an error: sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 3, and there are 1082 supplied. Ignore the rest of the code

CodePudding user response:

When you use executemany() the second argument should be a list of rows. So it should be

[(fullAddress, name, mailAddress), (fullAddress, name, mailAddress), (fullAddress, name, mailAddress), ...]

but you're creating

((fullAddress, fullAddress, fullAddress, ...), (name, name, name, ...), (mailAddress, mailAddress, mailAddress...)]

Use zip() to combine them in the proper way.

c.executemany(f"INSERT INTO users (fullAddress, name, mailAddress) VALUES (?, ?, ?)", 
    zip(fullAddress, nameList, addressList))
  • Related