-3

0

I am trying to create a function which is reading from my csv file and inserts it in a sql table.

This is my code:

def transaction_import(path):
   with open (path, 'r') as f:
       reader = csv.reader(f)
       columns = next(reader)
       query = 'insert into transactions({0}) values ({1})'
       query = query.format(','.join(columns), ','.join('?' * len(columns)))
       cursor = conn.cursor()
       for data in reader:
          cursor.execute(query, data)

transactions = transaction_import('../data/budapest.csv')

c.execute("select * from transactions")
transactions = c.fetchall()
for row in transactions:
   print(row)

What i want to do is to read several transactions from different csvs. All of them have the same structure and column names. ex: transactions = transaction_import('../Data/Source/ny.csv') transactions = transaction_import('../Data/Source/london.csv')

When I run it I get this error: File "/Users/.../main.py", line 82, in transaction_import cursor.execute(query,(data,)) sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 4, and there are 1 supplied.

deceze
  • 510,633
  • 85
  • 743
  • 889
AlexandraM
  • 27
  • 3
  • `print(data)` to see what the data is…? – deceze Jul 11 '22 at 07:54
  • i want to pass different arguments to my function. I already have print row in there – AlexandraM Jul 11 '22 at 08:24
  • 1
    The point is that apparently some rows don't contain enough columns. The first step when debugging something is to *output all the values* to see what the code is actually trying to do… – deceze Jul 11 '22 at 08:29

1 Answers1

-2

You're missing a comma in @ cursor.execute(query, data) Also google is your friend, see: sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 74 supplied