私は2つのsqliteテーブルにそれぞれ異なるアイテムとその数量が含まれています。私がしたいのは、最初のテーブルをループし、最初のテーブルから数量を選択し、アイテム名が一致する2番目のテーブルから数量を引きます。Pythonの2つの関数から値を減算する方法
(表1)
items | quantity ---------- sugar | 50 Pen | 10 apple | 50 berry | 1
二番目の表(ユーザ入力によって取り込ま)(表2)
items |quantity ---------- sugar |500 Pen |5 apple |8 berry |1
Iは表1をループしたいだから、私が言ったようにとの量を取得します砂糖を表1から抜き出し、表2に巡回して砂糖の量を求めてから、最後に値を減算し、減算を行った後に値を戻す。そして、同じことがテーブル1の他のアイテムに対しても行われます(ループ)
たとえば、砂糖を使用して、 (表2からの砂糖の量 - 表1からの砂糖の量) (500から50)= 450 そしてそれは私が
コード例の他の機能で使用可能な値450を返します。
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
私は上記のコード ファイルを実行したときに表示されるエラー "C:/ユーザー/メガマインド/ドキュメント/プログラミング/ Pychamプロジェクト/新しいフォルダ/ package.py" 引き算では、ライン22、 トン= 3 () - two() TypeError: - : 'タプル'と 'タプル'のためのサポートされていないオペランドタイプ
これを行うためのよりクリーンな方法があれば、どんな方法でも助けてください。
おそらく重複します:http://stackoverflow.com/questions/17418108/elegant-way-to-perform-tuple-arithmetic – meyer9
「[Pythonで2つの異なるsqlite3テーブルから値を減算する方法]の可能な複製」(http: /stackoverflow.com/questions/42942389/how-to-subtract-values-from-two-different-sqlite3-tables-in-python) – GurV
それを複製して申し訳ありません、私は以前のものを説明しませんでした。もう一度お尋ねください。お詫び申し上げます。 – Peter