-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_db.py
More file actions
executable file
·35 lines (26 loc) · 827 Bytes
/
build_db.py
File metadata and controls
executable file
·35 lines (26 loc) · 827 Bytes
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
import os
from config import db
from models import Item, ItemSchema
# Data to initialize database with
items = [
{"id": 0, "name":"first item"},
{"id": 1, "name":"second item"}
]
# Delete database file if it exists currently
if os.path.exists("items.db"):
os.remove("items.db")
# Create the database
db.create_all()
# iterate over the item structure and populate the database
for element in items:
print(element['name'])
item = Item(id = element['id'], name=element['name'])
db.session.add(item)
db.session.commit()
# let's check does items are in database
# Create the list of items from database
items = Item.query.order_by(Item.name).all()
# Serialize the data for the response
item_schema = ItemSchema(many=True)
data = item_schema.dump(items)
print(data)