Home > Back-end >  Insert multiple lists into table using sqlite3
Insert multiple lists into table using sqlite3

Time:08-18

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.

CodePudding user response:

    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]))
  • Related