0
votes

I am trying to insert a list of lists in a database but i get this error " cursor.execute('INSERT INTO Test VALUES(?, ?, ?)',list2) sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 3, and there are 200 supplied." in my list of lists there are 201 lists

import sqlite3
import csv
import pandas as pd
def Load():
    list1 = []
    comparar = []
    conexion = sqlite3.connect("Pruebas")
    cursor = conexion.cursor()
    cursor.execute('CREATE TABLE IF NOT EXISTS Test ( "id" INT NOT NULL , "User" TEXT NOT NULL , "Followed" INT NOT NULL , PRIMARY KEY (id))')
    cursor.execute("SELECT * FROM Test")
    list1 = cursor.fetchall()
    #print(data)    
    data = pd.read_csv(r'J:\\Proyectos y Trabajos\\Python\\Bot Instagram Follow\\Terminado BR\\Test.csv',delimiter=';')
    tuples = [tuple(x) for x in data.values]
    
    for i in tuples:
        if i not in list1:
            list1.append(i)
    list2 = [list(elem) for elem in list1]
    
    cursor.execute("DELETE FROM Test") 
    conexion.commit() 
    cursor.execute('INSERT INTO Test VALUES(?, ?, ?)',list2)
    conexion.commit() 
    conexion.close()
Load()
1
try cursor.execute('INSERT INTO Test VALUES(?, ?, ?)',(list2,)) and let me know but can you also say print(list2) and include it with the Q - Cool Cloud
VALUES(?, ?, ?) means you're trying to insert three values. Does list2 have exactly three values? - John Gordon
I get this error now: File "j:/Proyectos y Trabajos/Python/Bot Instagram Follow/Terminado BR/app.py", line 23, in Load cursor.execute('INSERT INTO Test VALUES(?, ?, ?)',(list2,)) sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 3, and there are 1 supplied. - Rodrigo Petrizzo
List2 is a list of 201 lists and eachone have 3 values inside like this '[201, 'rodrigo_petrizzo', 0]' - Rodrigo Petrizzo
That isn't how you insert it. One way would be to loop through the list and individually insert each piece. - juanpa.arrivillaga

1 Answers

1
votes

Iterate over list2 and perform an insert on the values in each sub-list:

for sublist in list2:
    cursor.execute('INSERT INTO Test VALUES(?, ?, ?)', sublist)
connexion.commit()