Python - how to parse and save JSON in MYSQL database

As the name implies, how to use python to elegantly access the API and parse and save the JSON content in a relational database (MYSQL) for later access?

Here I saved the data on the pandas object. But how do I create a mysql database, save json content on it and access the content for later use?

# Libraries
import json, requests
import pandas as pd
from pandas.io.json import json_normalize

# Set URL
url = 'https://api-v2.themuse.com/jobs'

# For loop to
for i in range(100):
    data = json.loads(requests.get(
        url=url,
        params={'page': i}
    ).text)['results']

data_norm = pd.read_json(json.dumps(data))
+4
source share
2 answers

Mysql , - Mysql Workbench CE. python . , data_norm, . insertDb() for, .

import MySQLdb

def dbconnect():
    try:
        db = MySQLdb.connect(
            host='localhost',
            user='root',
            passwd='password',
            db='nameofdb'
        )
    except Exception as e:
        sys.exit("Can't connect to database")
    return db

def insertDb():
    try:
        db = dbconnect()
        cursor = db.cursor()
        cursor.execute("""
        INSERT INTO nameoftable(nameofcolumn) \
        VALUES (%s) """, (data))
        cursor.close()
    except Exception as e:
        print e
0

, , varchar . jdata, JSON - , .

0

Source: https://habr.com/ru/post/1668358/


All Articles