Bonjour,

Le script que je créé, sert a récupérer des données de l'api OpenFoodFact pour les insérer dans une base Mysql.

J'ai deux problème que je n'arrive pas à gérer :
- lignes 39-46 je n'arrive pas a drop ma table

- Lignes 19-28 et 62-67 quand j'essaie de récupérer les données, ça me recupere bien la première category de l'api mais pas la suite, et j'ai ce message d'erreurs :

Traceback (most recent call last):
File "Classe.py", line 71, in <module>
create_dataBase = OpenFoodFact()
File "Classe.py", line 18, in __init__
self.get_category()
File "Classe.py", line 30, in get_category
self.data_base.insert_data_category(data[i])
File "Classe.py", line 66, in insert_data_category
self.cursor.execute(add_category)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/cursor_cext.py", line 266, in execute
raw_as_string=self._raw_as_string)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 475, in cmd_query
sqlstate=exc.sqlstate)
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'origine végétale')' at line 1
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 
# -*- coding: utf-8 -*-
import mysql.connector
import pprint
import requests
from Constant import *
 
 
 
class OpenFoodFact :
 
 
	def __init__(self):
		self.data_base = dataBaseMySql()
		self.data_base.create_data_base()
		self.get_category()
 
 
	def get_category(self):
		'''get category from API's openfoodfacts'''
		resp_category = requests.get('https://fr.openfoodfacts.org/categories&json=1')
		data_category_json = resp_category.json()
		data_tags_category = data_category_json.get('tags')
		data = [d.get('name') for d in data_tags_category]
 
 
		for i in range(10):
			self.data_base.insert_data_category(data[i])
 
 
 
class dataBaseMySql:
 
		def __init__(self):
			self.data_base = mysql.connector.connect(user=MYSQL_USER, password=MYSQL_PWD, host=MYSQL_HOST, database=MYSQL_DATABASE)
 
 
 
		def delete_table(self):
			self.cursor = self.data_base.cursor()
			self.cursor.execute("""
                                DROP TABLE IF EXISTS `Category`,
                                DROP TABLE IF EXISTS `Food`,
                                DROP TABLE IF EXISTS `Substitute`
                                """);
			self.data_base.commit()
 
		def create_data_base(self):
			self.cursor = self.data_base.cursor()
			self.cursor.execute("""
                                CREATE TABLE `Category` (
                                        `idCategory` int(11) NOT NULL AUTO_INCREMENT,
                                        `category` varchar(1000) NOT NULL,
                                        PRIMARY KEY (idCategory)
                                        )
                                        """);
			self.data_base.commit()
			self.cursor.close()
 
 
 
		def insert_data_category(self, category):
			self.cursor = self.data_base.cursor()
			add_category = ("INSERT INTO Category" "(category)" "VALUES('{}')".format(category))
			self.cursor.execute(add_category)
			self.data_base.commit()
			self.cursor.close()
 
 
create_dataBase =  OpenFoodFact()