MySQLdbを使用してMySQLデータベースにアクセスするためにPythonを使用しています。特定のテーブル「グローバル」からすべての行を取得したい
表 global には次の列があります。
regno
make
state
ユーザーは regno、make、および state の値を入力して、その特定の行のみを取得できます。入力しない場合は、すべての行が出力として表示されます。
私は次のコードを試しました:
import MySQLdb as db
from config.py import *
con = db.connect(server, user, pwd, database)
cur = con.cursor()
while(1):
print "-------Central Database-------"
print "Select : "
print "1. Balance Sheet\n2. Central Sheet"
choice = raw_input()
if choice == '3':
break
elif choice == '2':
regno = raw_input('Input Registration number (Blank for all) : ')
state = raw_input('Input state in/out (Blank for all) : ')
make = raw_input('Input make of the vehicle (Blank for all) : ')
if regno == '' and state == '' and make == '':
cur.execute("select * from global")
elif regno != '' and state != '' and make != '':
cur.execute("select * from global where regno=%s and state=%s and make=%s",(regno, state, make))
...
ご覧のとおり、これはすべての if-elif ステートメントにつながります。次のような MySQL クエリを直接使用できる方法はありますか?
select * from global where regno='' OR regno=%s