Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Insert multiple lists into table using sqlite3

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

    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]))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading