有没有更简单的方法可以导出到数据库?

时间:2019-10-04 09:24:05

标签: python-3.x postgresql sqlalchemy

我正在尽最大努力使用sqlalchemy将数据库初始导出(使用python-3.6)。

但是我达到的最好的想法只是将所有元素收集到字典中,因此我可以通过键轻松地引用它们。但是我不认为这是最好的方法。

import core.helpers.store_tables as st
import sqlalchemy as sa
import slugify

sku_set = set()

gender_table = []
brand_table = []
category_table = []
material_table = []
color_table = []
sport_table = []
shoe_table = []

gender_dict = {}
brand_dict = {}
category_dict = {}
material_dict = {}
color_dict = {}
sport_dict = {}
shoe_dict = {}

gender_id = 0
brand_id = 0
category_id = 0
material_id = 0
color_id = 0
sport_id = 0
shoe_id = 0

for item in batch:
    if item.gender not in gender_dict.keys():
        gender_element = st.Gender(id=gender_id, title=item.gender, slug=slugify.slugify(item.gender))
        gender_table.append(gender_element)
        gender_dict[item.gender] = gender_id
        gender_id += 1
    if item.brand not in brand_dict.keys():
        brand_element = st.Brand(id=brand_id, title=item.brand, slug=slugify.slugify(item.brand), size_group_id=0)
        brand_table.append(brand_element)
        brand_dict[item.brand] = brand_id
        brand_id += 1
    if item.category not in category_dict.keys():
        category_element = st.Subcategory(id=category_id, title=item.category,
                                          slug=slugify.slugify(item.category), category_id=0)
        category_table.append(category_element)
        category_dict[item.category] = category_id
        category_id += 1
    if item.material not in material_dict.keys():
        material_element = st.Material(id=material_id, title=item.material)
        material_table.append(material_element)
        material_dict[item.material] = material_id
        material_id += 1
    if item.color not in color_dict.keys():
        color_element = st.Color(id=color_id, title=item.color)
        color_table.append(color_element)
        color_dict[item.color] = color_id
        color_id += 1
    if item.sport not in sport_dict.keys():
        sport_element = st.Sport(id=sport_id, title=item.sport)
        sport_table.append(sport_element)
        sport_dict[item.sport] = sport_id
        sport_id += 1
    if item.sku not in sku_set:
        shoe_element = st.Shoe(id=shoe_id, title=item.title, description=item.description, sku=item.sku,
                               picture_url_1=item.picture_1, picture_url_2=item.picture_2, rating=0, views=0, clicks=0,
                               likes=0, discount=item.discount, min_price=item.price, num_shops=1,
                               color_id=color_dict[item.color], material_id=material_dict[item.material],
                               gender_id=gender_dict[item.gender], subcategory_id=category_dict[item.category],
                               brand_id=brand_dict[item.brand], sport_id=sport_dict[item.sport])
        shoe_id += 1
        sku_set.add(item.sku)
        shoe_table.append(shoe_element)

db_login = "postgres://usr:pass@localhost/test_db"
db = sa.create_engine(db_login)
session = sa.orm.sessionmaker(bind=db)
session = session(bind=db)

session.bulk_save_objects(gender_table)
session.bulk_save_objects(brand_table)
session.bulk_save_objects(category_table)
session.bulk_save_objects(color_table)
session.bulk_save_objects(material_table)
session.bulk_save_objects(sport_table)
session.bulk_save_objects(shoe_table)
session.commit()
session.close()

我没有收到错误,但是我认为这不是进行导出的最佳方法。而且由于我需要检查表中是否已存在参数,因此进行更新将更加困难。

0 个答案:

没有答案