TypeError:“ str”对象在MySQL中不可调用

时间:2019-10-25 17:28:35

标签: python

我正在尝试转换为str


import mysql.connector
from mysql.connector import Error

def VeriEkleme(Deger1,Deger2):
    try:
        connection = mysql.connector.connect(host='localhost',database='pythonregister',user='pyroot',password='')

        if connection.is_connected():
            print("MySQL bağlantısı aktif edildi.")

        mySql_insert_query = """INSERT INTO userinformations (Username, Password) VALUES """(Deger1,Deger2)
        cursor = connection.cursor()
        result = cursor.execute(mySql_insert_query)
        connection.commit()
        print("Record inserted successfully into Laptop table")
        cursor.close()

    except Error as e:
        print("Error while connecting to MysqL", e)

def Register():
    Username = input("Username:")
    Password = input("Pass:")
    VeriEkleme(str(Username),str(Password))


def EnterSystem():
    Login = "login"
    Answer = input("Login or Register?: ").lower()
    if Answer == Login:
        print("eşit")
        Register()
    else:
        EnterSystem()

EnterSystem()

Login or Register?: login
eşit
Username:s
Pass:s
MySQL bağlantısı aktif edildi.
Traceback (most recent call last):
  File "C:/Users/Relov/PycharmProjects/PygameProje1/PygameProjesi.py", line 36, in <module>
    EnterSystem()
  File "C:/Users/Relov/PycharmProjects/PygameProje1/PygameProjesi.py", line 32, in EnterSystem
    Register()
  File "C:/Users/Relov/PycharmProjects/PygameProje1/PygameProjesi.py", line 24, in Register
    VeriEkleme(str(Username),str(Password))
  File "C:/Users/Relov/PycharmProjects/PygameProje1/PygameProjesi.py", line 11, in VeriEkleme
    mySql_insert_query = """INSERT INTO userinformations (Username, Password) VALUES """(Deger1,Deger2)
TypeError: 'str' object is not callable

Process finished with exit code 1

1 个答案:

答案 0 :(得分:3)

您正在调用str,因为它是一个函数。

"""INSERT INTO userinformations (Username, Password) VALUES """(Deger1,Deger2)

我建议使用预处理语句。这样可以安全地防止SQL注入攻击。

mySql_insert_query = """INSERT INTO userinformations (Username, Password) VALUES (%s, %s)"""
cursor = connection.cursor()
result = cursor.execute(mySql_insert_query, (Deger1, Deger2))