我正在构建简单的帐户密码更改脚本,在执行此操作时,我看到了一些错误,即更改密码后,全局变量没有改变。我是否需要创建一个类或全局变量,以便在Account_ID, Account_Password.
我使用它时始终从 SQL 数据库获取最新信息,但这只是第一次,如果以后使用变量,它们不会更新。
try:
acc_username = input("* Please enter your username: ")
acc_password = getpass.getpass("* Please enter your password: ")
connection = psycopg2.connect(user="postgres",
password="postgres",
host="127.0.0.1",
port="5432",
database="postgres")
cursor = connection.cursor()
postgreSQL_select_Query = "select * from admins WHERE username=('%s') AND password = ('%s')" % (acc_username, acc_password)
cursor.execute(postgreSQL_select_Query) # PALEIST KOMANDA
adminu_data = cursor.fetchall() # READ DATA
# IF NON EXIST'S
if not adminu_data:
os.system('cls')
print("* Wrong password or username")
time.sleep(5)
os.system('cls')
continue
else:
for row in adminu_data:
# ADMIN INFO
global Admin_Id
Admin_ID = row[0]
global Admin_Username
Admin_Username = row[1]
global Admin_Password
Admin_Password = row[2]
global Admin_Email
Admin_Email = row[3]
print("* You're logged in as", Admin_Username)
time.sleep(3)
os.system('cls')
获得全局变量后,它们将在某处使用并且它们将工作,但如果我更改数据库中的帐户密码,我将得到相同的旧结果,或者我需要重新加载应用程序以获得新结果。