#cording:utf-8
#クエリー結果のtupleを分かりやすく表示
import wineshop
myhostdb = wineshop.getDB()
mycursor = myhostdb.cursor()
mycolor = input("ワインの色:")
myord = input("値段の降順:desc 昇順:asc 表示を指定して:")
sql = "select wineID,name as '品名',price as '値段',concat(locality,'・',country) as '産地' from wine inner join locality using(locaID) where color = %s " + "order by price " + myord
mycursor.execute(sql,(mycolor,))
print("\n\n{:3} {:20} {:5} {:5} {:16}".format("wID", "品名", "値段", "税込","産地"))
#unpacking the tuple by row
for wineID, 品名, 値段, 産地 in mycursor.fetchall():
print("{:3d} {:16} {:5d} {:5d} {:16}".format(wineID, 品名, 値段, int(値段*1.1), 産地))
myhostdb.close()
==== 実行例 ====
実行結果1
bash-4.2$ python3 query5.py
ワインの色:赤
値段の降順:desc 昇順:asc 表示を指定して:desc
wID 品名 値段 税込 産地
3 サンテミリオン 5800 6380 ボルドー・フランス
2 ジュヴレシャンべルタン 3000 3300 ブルゴーニュ・フランス
4 オーメドック 2200 2420 ボルドー・フランス
--------------------------------------------------------------
実行結果2
bash-4.2$ python3 query5.py
ワインの色:赤
値段の降順:desc 昇順:asc 表示を指定して:asc
wID 品名 値段 税込 産地
4 オーメドック 2200 2420 ボルドー・フランス
2 ジュヴレシャンべルタン 3000 3300 ブルゴーニュ・フランス
3 サンテミリオン 5800 6380 ボルドー・フランス
--------------------------------------------------------------
実行結果3
bash-4.2$ python3 query5.py
ワインの色:白
値段の降順:desc 昇順:asc 表示を指定して:asc
wID 品名 値段 税込 産地
1 シャブリ 2400 2640 ブルゴーニュ・フランス
5 サンセール 2800 3080 ロワール・ドイツ
6 シャンパン 4000 4400 シャンパーニュ・フランス
------------------------------------------------------