I'm working some code that inserts csv rows into an SQLite database using Python. I've been using it for awhile, and it's been working. However, there was an update to the CSV file but only rows were added, not columns. Now when I run the code, I receive "TypeError: not all arguments converted during string formatting". I've tried everything I could think of and find, and I still get this error. This is the code that executes the insertion portion of the database
fin = open("file.csv", "rb")
creader = csv.reader(fin, delimiter=',')
sql_insert = "INSERT INTO WR_Training VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
# Iterate through creader and insert the values into the table
# Pop off the 3 resources columns and insert the others
for row in creader:
row.pop(27)
row.pop(26)
row.pop(25)
if row[2] != "":
cu.execute(sql_insert, row)
fin.close()
cx.commit()
cx.close()
Originally, there are 29 columns all together, which is why I use row.pop to pop off the columns that I don't want, therefore I also don't include them in the original creation of the table as well. So it's storing 26 columns out of 29. I used to use %s instead of ? for the sql command, however %s didn't work and now ? doesn't work as well.
Any suggestions would be very helpful!
Thank you
rowhas the number of columns you think it should by looking atlen(row)? Are you certain this matches the number of?markers in your SQL statement?print(len(row))right before you execute to make sure it's as exactly long as you think it is. Python is telling you it's longer than 26 items.