gpt4 book ai didi

python - 类型错误 : 'str' object is not callable in MySQL

转载 作者:太空宇宙 更新时间:2023-11-04 11:09:49 25 4
gpt4 key购买 nike

我正在尝试转换为 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

最佳答案

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

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

我建议使用准备好的语句。这对于 SQL 注入(inject)攻击是安全的。

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

关于python - 类型错误 : 'str' object is not callable in MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58563144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com