Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. import mysql.connector
  2. import requests
  3. import os
  4. from dotenv import load_dotenv
  5. load_dotenv()
  6.  
  7. mydb = mysql.connector.connect(
  8.           host="localhost",
  9.           user=os.getenv("USER"),
  10.           passwd=os.getenv("PASSWORD")
  11.         )
  12.         mycursor = mydb.cursor()
  13.         mycursor.execute("DROP DATABASE food_database")
  14.         print("Création de la base de donnée...")
  15.         mycursor.execute("CREATE DATABASE food_database")
  16.         mycursor.execute("USE food_database")
  17.         mycursor.execute("CREATE TABLE category (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255))")
  18.         mycursor.execute("CREATE TABLE food (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), nutriscore_data INT, category_id INT, FOREIGN KEY (category_id) REFERENCES category(id))")
  19.  
  20.         mycursor.execute("INSERT INTO food (name, nutriscore_data) VALUES ('Nutella', '10')")  
  21.         mydb.commit()
  22.  
  23.         category_link = requests.get('https://fr.openfoodfacts.org/categories.json')
  24.         json_category = category_link.json()
  25.         product = json_category["tags"]
  26.         mycursor.execute("USE food_database")
  27.         for item in product[:50]:
  28.             category_name = str(item["name"])
  29.             add_employee = """INSERT INTO category(name) VALUES(%s)""" % (category_name)
  30.             # Insert new employee
  31.             mycursor.execute(add_employee)
  32.             print("ouais")
  33.             mydb.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement