2016-11-05 20 views
0

我有一個代碼來檢查他/她是否是一個管理員,但我如何檢查數據庫中的數字是否高於所需的?Sqlite3 Python - 檢查一個字段>整數

在表中的字段命名爲adminlevel

def login(): 
def loggedin(): 

    if adminlevel > 0: 
     print("Hello There Admin!") 
     print("Commands: Bet, Deposit, Logout") 
     print("Admin Commands: Ban, Give Credits") 
    else: 
     print("Hello there {}".format(username)) 
     print("Commands: Bet, Deposit, Logout") 

adminlevel = 0 
username = input("Please enter your username: ") 
password = getpass("Please enter your password: ") 
admin = c.execute("select 1 from accounts where adminlevel > 0") 
c.execute("select 1 from accounts where username = ? and password = ?", (username, password)) 
if c.fetchone(): 
    print('Hello there! {}'.format(username)) 
    loggedin() 
else: 

    print("Username and Password Not Found!") 
    login() 
+0

'...其中adminlevel> required_value'或'...其中adminlevel> required_value和...'。你想做什麼 ?也許你必須在一個'SELECT'中做?或者,也許你需要'SELECT * ...'來獲取行中的所有值,然後檢查一些值?它接縫你必須先學習SQL。 – furas

+0

如果adminlevel> 0 –

回答

0
if c.fetchone(): 
    c.execute("select 1 from accounts where adminlevel > 0") 
    if c.fetchone(): 
     print("Hello There Admin!") 
     print("Commands: Bet, Deposit, Logout") 
     print("Admin Commands: Ban, Give Credits") 
    else: 
     loggedin() 

只是做這個,忘了它的工作原理相同,我登錄。

+0

你可以在''SELECT * FROM accounts WHERE username =?AND password =?',然後'data = c.fetchone()'並檢查data ['adminlevel']> 0「(或類似的東西),這樣你只能向數據庫發出一個請求,也許在小程序中並不重要,但是在更大的情況下,我們會盡量減少對數據庫的請求。 – furas