Home > database >  Why are there brackets around the response from database?
Why are there brackets around the response from database?

Time:10-28

I was trying to understand the different values that we can get from database '''

        tmppass = db.execute("SELECT * from users WHERE username=:username", {"username": session['user_id']}).fetchone()
        tmppass_1 = db.execute("SELECT password from users WHERE username=:username", {"username": session['user_id']}).fetchone()
        old_password = request.form.get('old_password')
        newpass_1 = request.form.get('new_password_1')
        newpass_2 = request.form.get('new_password_2')
        hashOfNewPass = str(pbkdf2_sha256.hash(newpass_1))
        oldPassHash = pbkdf2_sha256.hash(old_password)
        print(tmppass['password'])
        print(tmppass_1)

'''

I am getting different results from the database for tmppass and tmppass_1

tmppass_1 = ('$pbkdf2-sha256$29000$bs1Z6z0HYOw9R4hR6t37nw$.ZtoRLUsZCYmkbRVNTiZt1uLLQwuJ.iyxrNcHg43SYA',)

tmppass['password'] = $pbkdf2-sha256$29000$bs1Z6z0HYOw9R4hR6t37nw$.ZtoRLUsZCYmkbRVNTiZt1uLLQwuJ.iyxrNcHg43SYA

[tmppass_1 which only gets the hash from the database is printing the string with the brackets][1]

CodePudding user response:

Generally speaking, (x,) in Python is a tuple with a single element.

Since you are using SELECT *, a tuple is returned (even if there is only one column in the table). It is a design choice in order to achieve consistency between all SELECT * queries regardless of the actual number of columns (specifically, to make sure existing code does not break if a column is later added to a table used in a SELECT * query).

  • Related