简体   繁体   English

如何从python中的两个函数中减去值

[英]How to subtract values from two functions in python

I have a two sqlite tables of which both contain different items and their quantities. 我有两个sqlite表,其中都包含不同的项目及其数量。 What I want to do is loop through the first table, select the quantity from the first table and subtract it from the quantity from the second table where the item names match. 我想做的是遍历第一个表,从第一个表中选择数量,然后从与项目名称匹配的第二个表中的数量中减去它。

(Table1) (表格1)

items  | quantity
----------
sugar  | 50
Pen    | 10
apple  | 50
berry  | 1

Second Table(populated by user input)(Table 2) 第二个表(由用户输入填充)(表2)

items  |quantity
----------
sugar  |500
Pen    |5
apple   |8
berry  |1

So as I said, I want to loop through table 1 and get the quantity of sugar from the Table 1. Then loop through Table 2 and get the quantity of sugar in table 2 too, then finally subtract the values and return the value after the subtraction has been carried out. 因此,正如我所说,我想遍历表1并从表1中获取糖的量。然后遍历表2并也获取表2中的糖的量,然后最后减去这些值并在进行了减法。 And then the same thing is done for the other items in the table1.(loop) 然后对table1中的其他项目执行相同的操作。(循环)

Eg Using sugar, (Quantity of Sugar from table 2 – Quantity of Sugar from table 1) (500 – 50) = 450 Then it returns the value 450 so I can use it in other functions 例如,使用糖,(表2中的糖数量–表1中的糖数量)(500 – 50)= 450然后它返回值450,因此我可以在其他函数中使用它

Code Example. 代码示例。

import sqlite3

db = sqlite3.connect('example.db')
#db.execute('create table Table1 (items text, quantity integer)')
#db.execute('create table Table2 (items text, quantity integer)')
a = 'Sugar'
b = 50
c = 500
db.execute('insert into Table1 (items, quantity) values(?,?)',(a, b))
db.execute('insert into Table1 (items, quantity) values(?,?)',(a, c))
db.commit()
db.close()

def one():
    cursor = db.execute('select items from Table1')
    for i in cursor.fetchall():
        return i

def two():
    cursor1 = db.execute('select quantity from Table1 where items = ?',(one()))
    for i in cursor1.fetchall():
        return i

def three():
    cursor1 = db.execute('select quantity from Table2 where items = ?',(one()))
    for i in cursor1.fetchall():
        return i

def subtraction():
    t = three() - two()
    return t

The error shown when i run the above code File "C:/Users/MegaMind/Documents/Programming/Pycham Projects/New folder/package.py", line 22, in subtraction t = three() - two() TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple' 当我运行上述代码文件“ C:/ Users / MegaMind / Documents / Programming / Pycham Projects / New folder / package.py”,第22行时,在减法运算中显示的错误t = three()-two()TypeError:不支持-的操作数类型:“元组”和“元组”

If there is a cleaner way to do this, please assist in any way you can. 如果有更清洁的方法可以这样做,请尽一切可能提供帮助。

You can simply tell the database to join the two tables: 您可以简单地告诉数据库联接两个表:

cursor = db.execute('''SELECT items, Table2.quantity - Table1.quantity
                       FROM Table1
                       JOIN Table2 USING (items)''')
for row in cursor:
    name = row[0]
    diff = row[1]
    ...

You're making it much too hard. 您太难了。 SQL can do that by itself and give you the results in a silver platter: SQL本身可以做到这一点,并为您提供银色的结果:

SELECT table1.items, table1.quantity - table2.quantity as remaining 
FROM table1 LEFT JOIN table2 ON table1.items = table2.items

The LEFT JOIN ensures that all rows of table1 will appear in the result. LEFT JOIN确保table1所有行都将出现在结果中。 If you only want the reduced counts, use plain JOIN . 如果只希望减少计数,请使用普通JOIN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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