· 6 years ago · May 14, 2019, 12:50 AM
1import sqlite3
2import json
3
4conn = sqlite3.connect('recipe.db')
5
6create_table_sql = """
7CREATE TABLE IF NOT EXISTS recipes (
8 ID INTEGER PRIMARY KEY AUTOINCREMENT,
9 title text,
10 rating real,
11 calories real,
12 protein real,
13 sodium real,
14 fat real,
15 categories text,
16 ingredients text,
17 directions text
18);
19"""
20
21c = conn.cursor()
22c.execute(create_table_sql)
23
24
25
26data = json.load(open('full_format_recipes.json'))
27
28
29
30for a_recipe in data:
31
32 if 'title' in a_recipe:
33
34 title = a_recipe['title']
35 rating = a_recipe['rating']
36 calories = a_recipe['calories']
37 protein = a_recipe['protein']
38 sodium = a_recipe['sodium']
39 fat = a_recipe['fat']
40 categories = "|".join(a_recipe['categories'])
41 ingredients = "|".join(a_recipe['ingredients'])
42 directions = "|".join(a_recipe['directions'])
43
44
45
46 row = (None,title,rating,calories,protein,sodium,fat,categories,ingredients,directions)
47 print(row)
48 dbr = c.execute('insert into recipes values (?,?,?,?,?,?,?,?,?,?)', row)
49 conn.commit()
50 print(dbr)
51 else:
52
53 continue