简体   繁体   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

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.

相关问题 Python中的sqlite3:当主键相同时,通过另一个表中的列更新一个表中的列 - sqlite3 in Python: Update a column in one table through a column from another table when the primary key is the same 一个表中的SQLite UPDATE字段基于另一表的外键 - SQLite UPDATE field in one table based on foreign key of another table python sqlite 如何使用代码中的附加数据将一行中的一些数据插入到另一张表中 - python sqlite how to insert some data from one row into another table with additional data from code sqlite3将主键数据从一个表插入到另一个表 - sqlite3 inserting primary key data from one table into another python + sqlite,将变量中的数据插入表中 - python + sqlite, insert data from variables into table 在插入pandas df的同时从另一个表插入外键 - INSERT foreign key from another table whilst inserting pandas df 将大量数据从一个表插入到另一个 MySQL Python - Insert bulk of data from one table to another MySQL Python Python Sqlite如何使用外键从不同的表中打印数据? - Python Sqlite how to print data from different table using foreign key? 在PostgreSQL中用executemany()在另一个表中插入外键 - INSERT FOREIGN KEY in another table with executemany() in PostgreSQL 在Django管理界面中插入到外键链接表 - Insert to foreign key-linked table in django admin interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM