4

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 requests, sqlite3
database = sqlite3.connect("ProjectDatabase.db")
cur = database.cursor()

d = requests.get("http://ergast.com/api/f1/2019/drivers")

I'm aiming to get the names and driver numbers of each driver and insert them all into a table called Drivers. (I'm also using more APIs with more tables, but if I figure out how to do one then the rest should be fine.) I assume I have to do something like

cur.execute('''INSERT INTO Drivers VALUES(?,?), (driverName, driverNumber)
''')

but I'm struggling to figure out how to insert the data straight into the table from the website. Does anyone know how to do this? Thanks

10
  • Is it necessary to parse the XML output? This particular API supports JSON response. Commented Mar 20, 2019 at 10:34
  • Try to just get the names from the website without putting them in a database first. Commented Mar 20, 2019 at 10:36
  • You correctly assume. The normal way has 3 steps: 1/ get the data 2/ parse the data 3/ insert the relevant parts into the database. Commented Mar 20, 2019 at 10:36
  • I don't quite see where you struggle to be honest. It basically goes as follow: request -> parse -> insert at what point do you struggle? Commented Mar 20, 2019 at 10:37
  • @LukaszSalitra I don't know, I used the 'requests.get()' and that's what it automatically gave me. If JSON is needed then that's fine, I'd just need to figure out how to get the JSON response instead of XML. Commented Mar 20, 2019 at 10:38

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

I've edited the loop into the answer. You can basically take that answer as posted and put it into your code, shouldn't need too much editing, except ofcourse the db connection and cursor creation.
Thanks, I've tried to use this but it's throwing up an error saying there's no such column "name" (it doesn't with "driverName" either [same with "number"]) - I tried to specify the columns I'm entering the data into but it still throws up this error.
@Grace have you changed the url and added the drivers = d["MRData"]["DriverTable"]["Drivers"] part? It should work given you've copied the complete answer.
I have, I've just double checked and it's the same as yours.
@Grace yeah I just saw I've goofed with the sql statement, this way it should work and the way I usually do it. note: in the sql statement I name the columns I want to add, in my test db the column names were name and number.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.