简体   繁体   English

Python“TypeError:'str'对象不可调用”-我没有在代码中使用“str”

[英]Python "TypeError: 'str' object is not callable" - I'm not using "str" in code

I am getting a TypeError: 'str' object is not callable in the following piece of code on the line beginning with "exists":我收到一个 TypeError: 'str' object is not callable 在以下以“exists”开头的代码段中:

@app.route('/passwordLoginForm1', methods=['GET','POST'])
def passwordLoginForm1():
    username = request.form['username']
    password = request.form['password']
    exists = db.get_passdetails(username,password)

    if exists == 'true':
            return render_template('pin.html') 
    else:
            return render_template('passwordsLogin2.html')

This is the get_passdetails for reference:这是 get_passdetails 供参考:

def get_passdetails(username,password):
    cur=conn.cursor()
    cur.execute("SELECT *  FROM Password WHERE username = %s AND password = %s" (username, password))
    details = cur.fetchone()
    if details:
        return 'true'
    else:   
        return 'false

Relatively new to Python so any help would be appreciated Python相对较新,因此将不胜感激

I believe the error is actually in this expression:我相信错误实际上是在这个表达式中:

"SELECT *  FROM Password WHERE username = %s AND password = %s" (username, password)

Since there's no operator between the string and the (...) , it's being interpreted as a function call on a string (which naturally produces an error since a string isn't a function).由于字符串和(...)之间没有运算符,因此它被解释为对字符串的函数调用(由于字符串不是函数,因此自然会产生错误)。 You meant to put a % in between.你的意思是在两者之间加上一个%

use ?利用 ? instead of %s and don't forget to add comma,而不是 %s 并且不要忘记添加逗号,

cur.execute("SELECT *  FROM Password WHERE username = ? AND password = ?" ,(username, password))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM