Skip to content
Advertisement

How do I get the data extracted from API to my database

I am currently working on a project to build a database on professor’s research paper database. This is my first time building a database(never had experience with MYSQL) and I am learning as I am doing it.

I was able to use an api to get the data, for example:

{“authorId”: “1773022”, “url”: “https://www.semanticscholar.org/author/1773022”, “papers”: [{“paperId”: “1253d2704580a74e776ae211602cfde71532c057”, “title”: “Nonlinear Schrodinger Kernel for hardware acceleration of machine learning”}, {“paperId”: “71f49f1e3ccb2e92d606db9b3db66c669a163bb6”, “title”: “Task-Driven Learning of Spatial Combinations of Visual Features”}, {“paperId”: “bb35ae8a50de54c9ca29fbdf1ea2fbbb4e8c4662”, “title”: “Statistical Learning of Visual Feature Hierarchies”}]}

How would I use python to turn this into a table so I can use it to build my database?

I am trying to make a table where columns are: Paper ID|Title|

Advertisement

Answer

From https://www.w3schools.com/python/python_mysql_getstarted.asp

Install:


Over view

Create a connection:

Create a cursor to interact with the connection you made, then create a Database:

After the database has been created, you can start connecting with it like so:

Note that you don’t have to actually close your connection and reopen it to connect to that database, yet I don’t see the docs mentioning anything about interacting with that specific database after you’ve created it, so I’m going to close it after I create the database…

Create the table with the proper datatypes and constraints:

Then insert into it and commit the entries:

Close the connection:


Your Specific Case

This is the data you have provided:

Granted that I don’t know all the details, but based on the data given, I’d assume that you would want a table for:

  1. Authors – id (auto increment pk), authorId (varchar), url (varchar),
  2. Papers – id (auto increment pk), authorId (varchar fk), paperId, title (varchar)

Modify this as you please:

Side note:

I have not tested this. I will be amazed myself if this works first try. If you come across any errors, I can do my best to trouble shoot.


EDIT:

Once again, I have not tested this. It may not work, but the idea is there. Please check out this article: What are some good Python ORM solutions?

Save this as Models.py

Usage:

This may not even work – but I did it to give you some hope. There are easier ways to insert data into your database. You need to look into ORMs – the django (a webserver library for python) library has one native to it, that makes it super easy to manage your database.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement