Skip to content Skip to sidebar Skip to footer

Insert Data Into Sqlite3 Database With Api

I'm trying to insert data from a web API into my database (I am using sqlite3 on python 3.7.2) and I can't find any tutorials on how do to so. So far all my code is: import request

Solution 1:

As stated in the comment section in the OP, the problem seemed to be how to parse the API point.

d = requests.get("http://ergast.com/api/f1/2019/drivers.json")
d = d.json()
drivers = d["MRData"]["DriverTable"]["Drivers"]

would be the answer to that question to access all drivers provided by that API.

To add the entries to the db you can use the following:

for dr in drivers:
    name = dr["familyName"]
    number = dr["permanentNumber"]
    sql = 'INSERT INTO Drivers (name,number) VALUES(?,?)'val = (name,number)
    cur.execute(sql,val)

with this solution you don't have to use specify the index and can directly access the parameter you're interested in.

Post a Comment for "Insert Data Into Sqlite3 Database With Api"