Insert multiple lists into table using sqlite3

Question:

I’m trying to insert two lists into the table where one list is inserted into one column and the other into another column where item 1 from list 1 is held in the same record as item 1 in list 2. The length of both lists is 73.

airfields = [‘Andrewsfield’,’Bagby’,’Beccles’…]
icao_codes = [‘EGSL’,’EGNG’,’EGSM’…]

AirfieldID Name ICAO
1 Andrewsfield EGSL
2 Bagby EGNG
3 Beccles EGSM

This is my code, but I get an error message when I run it:

Traceback (most recent call last):
  File "main.py", line 33, in <module>
    data_entry()
  File "main.py", line 30, in data_entry
    c.execute("""INSERT INTO Airfields (Name, ICAO) VALUES (?, ?)""", (airfields[count],), (icao_codes[count],))
TypeError: function takes at most 2 arguments (3 given)
def data_entry():
   count = 1
   while count != 73
      c.execute("""INSERT INTO Airfields (Name, ICAO) VALUES (?, ?)""", (airfields[count],), (icao_code[count],))
      count = count + 1

Any help would be appreciated.

Asked By: golem

||

Answers:

    c.execute("""INSERT INTO Airfields (Name, ICAO) VALUES (?, ?)""", (airfields[count],), (icao_codes[count],))
TypeError: function takes at most 2 arguments (3 given)

execute takes its values as a tuple. You’ve passed two tuples, (airfields[count],) and (icao_codes[count],). With the SQL statement that means 3 arguments, execute takes 2.

You’d use one tuple, a single set of parenthesis, for all values: (airfields[count], icao_codes[count]).

c.execute("""INSERT INTO Airfields (Name, ICAO) VALUES (?, ?)""", (airfields[count], icao_codes[count]))

Note: You can write your loop better with for x in range.

 for count in range(1, 72):
   c.execute("""INSERT INTO Airfields (Name, ICAO) VALUES (?, ?)""", (airfields[count], icao_code[count]))
Answered By: Schwern
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.