Skip to content

Instantly share code, notes, and snippets.

@ihopeudie
Created May 26, 2025 19:10
Show Gist options
  • Save ihopeudie/8e67fbab78d9bcb84388a719461abc01 to your computer and use it in GitHub Desktop.
Save ihopeudie/8e67fbab78d9bcb84388a719461abc01 to your computer and use it in GitHub Desktop.
module 4
# Метод должен принимать идентификатор типа продукции, идентификатор типа материала,
# количество продукции – целые числа,
# параметры продукции (два параметра) – вещественные, положительные числа,
# а возвращать целое число – количество сырья с учетом возможных потерь сырья.
import mysql.connector
def create_connection():
return mysql.connector.connect(
host="localhost",
user="root",
password="1234",
database="Test1"
)
def calc_material(product_id, material_id, amount, param1, param2):
query = """
select ((%s * %s + pt.koefficient) + (%s * %s + pt.koefficient) * mt.lost_percent) * %s
from product p
left join product_type pt on p.product_type_id = pt.id
left join material_type mt on p.material_type_id = mt.id and mt.id = %s
where p.id = %s
"""
conn = create_connection()
cursor = conn.cursor()
cursor.execute(query, [param1, param2, param1, param2, amount, material_id, product_id])
result = cursor.fetchone()
if not result:
return -1
print(result[0])
cursor.close()
conn.close()
return result[0]
if __name__ == '__main__':
calc_material(1, 2, 2, 1.0, 2.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment