2014-12-07 4 views
-1

cursor.fetchone()을 사용하여 튜플을 반환한다는 것을 알고있는 sqlite3 데이터베이스에서 데이터를 가져 오려고합니다. CGI 스크립트는 다른 프로그램은 데이터를 제출하고 나는 그들이 일치하지 않습니다에 가져 가서 내가 그들을 비교하려고 그렇게 할 때 자신의 암호에로에게 2 경기를 인쇄 :두 개의 튜플은 인쇄 할 때 동일하고 유니 코드에 있지만 비교하면 파이썬 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 오브젝트로 디코딩되지 ** 않습니다. –

답변

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

정말 고맙습니다. –