def add_descriptor(name, category, quantity):
""" Add price descriptor
:param str name: Price name
:param int category: category id
:param int quantity: The quantity (in mL) of the product
:return int: Return price id created or None
"""
with Database() as database:
database.transaction()
cursor = QtSql.QSqlQuery(database)
cursor.prepare("INSERT INTO price_description(label, category, quantity) \
VALUES(:label, :category, :quantity)")
cursor.bindValue(":label", name)
cursor.bindValue(":category", category)
cursor.bindValue(":quantity", quantity)
if cursor.exec_():
desc_id = cursor.lastInsertId()
cursor.prepare("SELECT id FROM products WHERE category=:category")
cursor.bindValue(":category", category)
if cursor.exec_():
while cursor.next():
pcurser = QtSql.QSqlQuery(database)
pcurser.prepare("INSERT INTO prices(price_description, \
product, value) VALUES(:desc, :product, :value)")
pcurser.bindValue(":desc", desc_id)
pcurser.bindValue(":product", cursor.value('id'))
pcurser.bindValue(":value", 0.00)
pcurser.exec_()
database.commit()
return desc_id
# Something went wrong
database.rollback()
return None
评论列表
文章目录