I am trying to migrate data from CSV to SQLite database. The CSV has 3 columns and table has only 2. I tried to pop() out the last element in the list returned by row.split(',') but it is giving a different error
code:
seen = set();
with open("E:/Forage/Walmart/task4/shipping_data_1.csv",'r') as file:
for row in file:
if row in seen: continue
seen.add(row)
cur.execute("INSERT INTO product values(?,?)",row.split(',').pop())
con.commit();
con.close()
error: when
cur.execute("INSERT INTO product values(?,?)",row.split(',')); is used
-->Incorrect number of bindings supplied. The current statement uses 2, and there are 3 supplied.
and
cur.execute("INSERT INTO product values(?,?)",row.split(',').pop()); is used
-->Incorrect number of bindings supplied. The current statement uses 2, and there are 8
supplied.
CodePudding user response:
.pop() is not what you want (it returns the popped element - the third column, and then Python is trying to iterate over letters in the string). You need to use slicing instead. If you want to keep the first two columns, do
row.split(',')[:2]