[英]insert into 2 table one linked to the second through FOREIGN KEY and take data from another table in sqlite and python
I have 3 tables and this is the scheme for this table我有 3 张桌子,这是这张桌子的方案
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我必须在 products 中输入产品的 id
And number of products和产品数量
And the prix should be extracted from the product table and multiplied by the number of products并且应该从产品表中提取prix并乘以产品数量
the same for margin保证金相同
and the general bill id have to be the same as the general_bill并且一般账单 ID 必须与 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之后,使用来自表明细账单的具有相同 id 的总利润和总利润的信息更新一般账单
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
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.