無法修改用戶
问题描述:無法修改用戶
相关代码:
for index in range(len(result)):
TypeError: object of type 'NoneType' has no len()相关代码:
相关代码:
# 查詢用戶分頁紀錄
def search_list(self, page):
try:
con = pool.get_connection()
cursor = con.cursor()
sql = 'SELECT u.id, u.username, r.role ' \
'FROM t_user u JOIN t_role r ' \
'ON u.role_id = r.id ' \
'ORDER BY u.id ' \
'LIMIT %s, %s'
cursor.execute(sql, ((page-1)*10), 10) # 起始值, 一頁顯示10條數據
result = cursor.fetchall()
return result
except Exception as e:
print(e)
finally:
# dir() 函数的作用是返回当前范围(当前模块)内的变量、方法和定义的类型列表获得的属性列表。
if 'con' in dir():
con.close()
相关代码:
elif opt == '2':
page = 1 # 必需要將變量設在循環外部, 否則每一次循環, 上一次搜索到的頁數將消失
while True:
os.system('cls')
count_page = __user_service.search_count_page() # 查詢總頁數
result = __user_service.search_list(page) # 所有紀錄的結果
for index in range(len(result)):
one = result[index] # 需要逐一提取數據 -主鍵, title, content_id, type_id
print(Fore.LIGHTBLUE_EX,
'\n\t%d\t%s\t%s' % (index + 1, one[1], one[2]))
print(Fore.LIGHTBLUE_EX, '-----------------------------')
print('\n\t%d/%d' % (page, count_page))
print(Fore.LIGHTBLUE_EX, '-----------------------------')
print(Fore.LIGHTRED_EX, "\n\tback.返回上一層")
print(Fore.LIGHTRED_EX, "\n\tprev.上一頁")
print(Fore.LIGHTRED_EX, "\n\tnext.下一頁")
print(Style.RESET_ALL)
opt = input('\n\t請輸入操作編號:')
if opt == 'back':
break
elif opt == 'prev' and page > 1:
page -= 1 # 需不需要重新調用查詢功能? 不用! 因為page會保存在循環內, 到時會重新執行
elif opt == 'next' and page < count_page: # 當前頁數 < 總頁數
page += 1
elif int(opt) >= 1 and int(opt) <= 10: # 此處的opt要輸入控制台顯示的新聞序號, 一頁10條紀錄
os.system('cls')
user_id = result[int(opt) - 1][0]
username = input('\n\t新用戶名:')
password = getpass('\n\t密碼:')
repassword = getpass('\n\t重複密碼:')
if password != repassword:
# 用戶提示
print(Fore.LIGHTRED_EX, '\n\t兩次密碼不一致(3秒後自動返回)')
print(Style.RESET_ALL)
time.sleep(3)
break # 結束這次判斷
result = __role_service.search_list()
email = input('\n\t請輸入新郵箱:')
for index in range(len(result)):
one = result[index] # 使序號連續
print(Fore.LIGHTBLUE_EX, '%d.%s' % (index + 1, one[1]))
print(Style.RESET_ALL)
opt = input('\n\t角色編號;')
role_id = result[int(opt)-1][0]
opt = input('\n\t是否保存(Y/N)')
if opt == 'Y' or opt == 'y':
__user_service.update(id, username, password, email, role_id)
print('\n\t保存成功(3秒後自動返回)')
time.sleep(3)8
收起
正在回答 回答被采纳积分+1
1回答
精慕门_learner
2022-06-24 23:08:09
elif opt == '2':
page = 1 # 必需要將變量設在循環外部, 否則每一次循環, 上一次搜索到的頁數將消失
while True:
os.system('cls')
count_page = __user_service.search_count_page() # 查詢總頁數
result = __user_service.search_list(page) # 所有紀錄的結果
for index in range(len(result)):
one = result[index] # 需要逐一提取數據 -主鍵, title, content_id, type_id
print(Fore.LIGHTBLUE_EX,
'\n\t%d\t%s\t%s' % (index + 1, one[1], one[2]))
print(Fore.LIGHTBLUE_EX, '-----------------------------')
print('\n\t%d/%d' % (page, count_page))
print(Fore.LIGHTBLUE_EX, '-----------------------------')
print(Fore.LIGHTRED_EX, "\n\tback.返回上一層")
print(Fore.LIGHTRED_EX, "\n\tprev.上一頁")
print(Fore.LIGHTRED_EX, "\n\tnext.下一頁")
print(Style.RESET_ALL)
opt = input('\n\t請輸入操作編號:')
if opt == 'back':
break
elif opt == 'prev' and page > 1:
page -= 1 # 需不需要重新調用查詢功能? 不用! 因為page會保存在循環內, 到時會重新執行
elif opt == 'next' and page < count_page: # 當前頁數 < 總頁數
page += 1
elif int(opt) >= 1 and int(opt) <= 10: # 此處的opt要輸入控制台顯示的新聞序號, 一頁10條紀錄
os.system('cls')
user_id = result[int(opt) - 1][0]
username = input('\n\t新用戶名:')
password = getpass('\n\t密碼:')
repassword = getpass('\n\t重複密碼:')
if password != repassword:
# 用戶提示
print(Fore.LIGHTRED_EX, '\n\t兩次密碼不一致(3秒後自動返回)')
print(Style.RESET_ALL)
time.sleep(3)
break # 結束這次判斷
result = __role_service.search_list()
email = input('\n\t請輸入新郵箱:')
for index in range(len(result)):
one = result[index] # 使序號連續
print(Fore.LIGHTBLUE_EX, '%d.%s' % (index + 1, one[1]))
print(Style.RESET_ALL)
opt = input('\n\t角色編號;')
role_id = result[int(opt)-1][0]
opt = input('\n\t是否保存(Y/N)')
if opt == 'Y' or opt == 'y':
__user_service.update(id, username, password, email, role_id)
print('\n\t保存成功(3秒後自動返回)')
time.sleep(3)
# 查詢用戶分頁紀錄
def search_list(self, page):
try:
con = pool.get_connection()
cursor = con.cursor()
sql = 'SELECT u.id, u.username, r.role ' \
'FROM t_user u JOIN t_role r ' \
'ON u.role_id = r.id ' \
'ORDER BY u.id ' \
'LIMIT %s, %s'
cursor.execute(sql, ((page-1)*10), 10) # 起始值, 一頁顯示10條數據
result = cursor.fetchall()
return result
except Exception as e:
print(e)
finally:
# dir() 函数的作用是返回当前范围(当前模块)内的变量、方法和定义的类型列表获得的属性列表。
if 'con' in dir():
con.close()
Python全能工程师
- 参与学习 人
- 提交作业 16416 份
- 解答问题 4469 个
全新版本覆盖5大热门就业方向:Web全栈、爬虫、数据分析、软件测试、人工智能,零基础进击Python全能型工程师,从大厂挑人到我挑大厂,诱人薪资在前方!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星