繁体   English   中英

插入到通过 FOREIGN KEY 链接到第二个表的 2 个表中,并从 sqlite 和 python 中的另一个表中获取数据

[英]insert into 2 table one linked to the second through FOREIGN KEY and take data from another table in sqlite and python

我有 3 张桌子,这是这张桌子的方案

在此处输入图像描述

第一个是拥有所有产品的产品以及该产品的价格和利润

第二个是包含客户信息和总金额的一般账单

第三个是问题;

我必须在 products 中输入产品的 id

和产品数量

并且应该从产品表中提取prix并乘以产品数量

保证金相同

并且一般账单 ID 必须与 general_bill 相同

之后,使用来自表明细账单的具有相同 id 的总利润和总利润的信息更新一般账单

现在我只弄清楚最简单的事情

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

我找到了一个适合我的解决方案,我不知道它是否是最好的,或者是否有办法让它更容易,但这是我的解决方案

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM