![](/img/trans.png)
[英]sqlite3 in Python: Update a column in one table through a column from another table when the primary key is the same
[英]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.