2014-12-07 45 views
-1

我想從sqlite3數據庫中獲取數據,我知道返回元組使用cursor.fetchone()但由於某種原因,當用戶在其它方案,這是一個CGI腳本提交數據,我把它並打印了2投其所好作爲他們的密碼,所以當我試圖比較他們,他們永遠不會匹配:2個元組相同時打印和unicode相同,但比較時,他們不匹配Python 2.7

#!/usr/bin/python 

import sqlite3 as lite 
import cgi 

db = lite.connect('qwerty0987654321.db') 
usrnme = "none" 
passwd = "none" 
passver = "none" 

def main(): 
    global usrnme 
    global passwd 
    print "Content-type:text/html\r\n\r\n" 
    print "<html>" 
    print "<head><title>Profile</title></head>" 
    print "<body>" 

    form = cgi.FieldStorage() 
    if form.getvalue('username'): 
     usrnme = form.getvalue('username') 
     if form.getvalue('passwd'): 
      passwd = form.getvalue('passwd') 
      if isauth() is True: 
       print "Welcome %s" % usrnme 
      elif isauth() is False: 
       print "Wrong username or password!" 
     else: 
      print "No Password!" 
    else: 
     print "No Username!" 
    print '</body>' 
    print '</html>' 

def isauth(): 
    global usrnme, passwd, passver 
    c = db.cursor() 
    c.execute("SELECT password FROM Users WHERE username = ?",(usrnme,)) 
    passver = c.fetchone()[0] 
    passver = tuple(passver,) 
    passwd = tuple(passwd[0],) 
    if cmp(passwd,passver) == 0: 
     return True 
    else: 
     print(passver,passwd) 
     return False 


if __name__ == '__main__': 
    main() 
+0

那麼,什麼*是*打印呢?請向我們展示'print'語句的輸出。通常,CGI帖子無論如何都不會解碼爲「unicode」對象。 – 2014-12-07 02:19:06

回答

0

它看起來像你的錯誤是這裏:passwd[0]。因爲str可以被索引,所以它會引用str中第一個位置的字符。這將是'n'

passver = c.fetchone()[0] # get the first field in the first item in the result set 
passver = tuple(passver,) # add it to a tuple. 
passwd = tuple(passwd[0],) # add passwd[0] (n) to a tuple 

這將無法正常工作。嘗試改爲:

passver = c.fetchone()[0] # get the first field in the first item in the result set 
passver = tuple(passver,) # add it to a tuple. 
passwd = tuple(passwd,) # add all of passwd to a tuple 
# comparrison should succeed depending on contents of Users 
+0

非常感謝你這樣做了。 – 2014-12-07 02:26:06

相關問題