I have 3 tables and this is the scheme for this table
the first one is the product that has all products and the price and the margin of this product
the sconde one is the general bill that have information about the client and the total
The 3rd one is the problem;
I have to enter the id of the product in products
And number of products
And the prix should be extracted from the product table and multiplied by the number of products
the same for margin
and the general bill id have to be the same as the general_bill
and after that update the general bill with information about the total and total profit that have the same id from the table detail bill
for now I only figure out the simplest thing
import sqlite3
import time, datetime
from datetime import timedelta
class Crud_db:
def __init__(self, database = 'database.db'):
self.database = database
def connect(self):
self.connection = sqlite3.connect(self.database)
self.cursor = self.connection.cursor()
print('connect seccesfully')
def execute(self, query):
self.query = query
self.cursor.execute(self.query)
def close(self):
self.connection.commit()
self.connection.close()
def create_tables(self):
# create all tables
def insert_new_bill(self):
self.connect()
date_f = str(datetime.date.today())
time_f = str(datetime.datetime.now().time())
client_name = input('client name: ')
query01 = 'INSERT INTO general_bill (client_name, date_g, time_g) VALUES (?, ?, ?)'
data = (client_name,date_f, time_f)
self.cursor.execute(query01,data)
self.close()
print('added to general bill ..!')
def add_product(self):
self.connect()
product_name = input('product name: ')
prix = float(input('the price : '))
royltie = float(input('profit: '))
product_discreption = input('discreption: ')
product_query = 'INSERT INTO product (product_name, prix, royltie, product_descreption) VALUES (?,?,?,?)'
data_set = [product_name,prix,royltie,product_discreption]
self.cursor.execute(product_query,data_set)
self.close()
print(f'product {product_name} added to database')
question = input('do you wana add more products ?(yes/no): ')
if question.lower() == 'yes':
self.add_product()
else:
pass
CodePudding user response:
I find a solution that work for me, I don't know if it is the best one, or if there is a way to make it easier but this is my solution
def insert_new_bill(self):
self.connect()
date_f = str(datetime.date.today())
time_f = str(datetime.datetime.now().time())
client_name = input('client name: ')
query01 = 'INSERT INTO general_bill (client_name, date_g, time_g) VALUES (?, ?, ?)'
data = (client_name,date_f, time_f)
self.cursor.execute(query01,data)
print('added to general bill ..!')
# add the detail of this bill
question = 'yes'
while question == 'yes':
product = input('product id: ')
number_of_product = input('number of product: ')
query2 = 'INSERT INTO details_bill (products, number_of_products, prix, royltie, date,time, general_bill_id) VALUES (?,?,?*(select prix from product where id =?),?*(select royltie from product where id =?),?,?,(select max(id) from general_bill where client_name = ?))'
data_query_2 = (product, number_of_product, number_of_product, product, number_of_product, product, date_f, time_f, client_name)
self.cursor.execute(query2,data_query_2)
question = input('do you wana add more product for this client (yes/no): ')
else:
query_3 = 'UPDATE general_bill SET total = (SELECT SUM(prix) FROM details_bill WHERE general_bill_id = (select max(id) from general_bill where client_name = ?) ), number_of_products = (SELECT SUM(number_of_products) FROM details_bill WHERE general_bill_id = (select max(id) from general_bill where client_name = ?) ) WHERE id = (select max(id) from general_bill where client_name = ?)'
data_query_3 = (client_name, client_name, client_name)
self.cursor.execute(query_3,data_query_3)
print(f'all product that |{client_name}| buy added to database seccesfully')
self.close()