1. 程式人生 > 其它 >MySQL(11) - Python+MySQL開發新聞管理系統

MySQL(11) - Python+MySQL開發新聞管理系統

1.前置知識及條件

1.1.資料庫指令碼

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for t_news
-- ----------------------------
DROP TABLE IF EXISTS `t_news`;
CREATE TABLE `t_news` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(40) NOT NULL,
  `editor_id` int(10) unsigned NOT
NULL, `type_id` int(10) unsigned NOT NULL, `content_id` char(12) NOT NULL, `is_top` tinyint(3) unsigned NOT NULL, `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `state` enum('草稿','待審批','已審批','隱藏') NOT NULL, PRIMARY
KEY (`id`), KEY `editor_id` (`editor_id`), KEY `type_id` (`type_id`), KEY `state` (`state`), KEY `create_time` (`create_time`), KEY `is_top` (`is_top`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of t_news -- ----------------------------
INSERT INTO `t_news` VALUES ('1', '新聞標題1', '2', '1', '1', '1', '2018-11-22 18:55:56', '2018-11-22 18:55:56', '待審批'); -- ---------------------------- -- Table structure for t_role -- ---------------------------- DROP TABLE IF EXISTS `t_role`; CREATE TABLE `t_role` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `role` varchar(20) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `role` (`role`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of t_role -- ---------------------------- INSERT INTO `t_role` VALUES ('2', '新聞編輯'); INSERT INTO `t_role` VALUES ('1', '管理員'); -- ---------------------------- -- Table structure for t_type -- ---------------------------- DROP TABLE IF EXISTS `t_type`; CREATE TABLE `t_type` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `type` varchar(20) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `type` (`type`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of t_type -- ---------------------------- INSERT INTO `t_type` VALUES ('2', '體育'); INSERT INTO `t_type` VALUES ('5', '歷史'); INSERT INTO `t_type` VALUES ('4', '娛樂'); INSERT INTO `t_type` VALUES ('3', '科技'); INSERT INTO `t_type` VALUES ('1', '要聞'); -- ---------------------------- -- Table structure for t_user -- ---------------------------- DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `username` varchar(20) NOT NULL, `password` varchar(500) NOT NULL, `email` varchar(100) NOT NULL, `role_id` int(10) unsigned NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`), KEY `username_2` (`username`) ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of t_user -- ---------------------------- INSERT INTO `t_user` VALUES ('1', 'admin', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', '[email protected]', '1'); INSERT INTO `t_user` VALUES ('2', 'scott', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', '[email protected]', '1'); INSERT INTO `t_user` VALUES ('3', 'test_1', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', '[email protected]', '2'); INSERT INTO `t_user` VALUES ('4', 'test_2', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', '[email protected]', '2'); INSERT INTO `t_user` VALUES ('5', 'test_3', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', '[email protected]', '2'); INSERT INTO `t_user` VALUES ('6', 'test_4', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', '[email protected]', '2'); INSERT INTO `t_user` VALUES ('7', 'test_5', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', '[email protected]', '2'); INSERT INTO `t_user` VALUES ('8', 'test_6', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', '[email protected]', '2'); INSERT INTO `t_user` VALUES ('9', 'test_7', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', '[email protected]', '2'); INSERT INTO `t_user` VALUES ('10', 'test_8', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', '[email protected]', '2'); INSERT INTO `t_user` VALUES ('11', 'test_9', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', '[email protected]', '2'); INSERT INTO `t_user` VALUES ('12', 'test_10', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', '[email protected]', '2'); INSERT INTO `t_user` VALUES ('13', 'test_11', '3E6BC27A781F0AC08BCFD78CC3DCE4CA', '[email protected]', '2');

1.2.python相關元件安裝

1.2.1.更新pip

python -m pip install --upgrade pip

注意:如果Python環境安裝在C盤,必須用管理員身份開啟終端窗口才能升級pip,以及安裝模組

1.2.2.安裝Colorama模組

安裝:

Python程式向控制檯輸出彩色文字,先要安裝colorama模組

pip install colorama

基礎使用:


1.3.專案描述

1.3.1.專案結構描述

專案採用的是DAO設計模式,封裝資料層的呼叫,封裝資料庫的操作,如果後續表格發生變動,只需要修改dao對應的檔案即可,不需要全部修改。

什麼是DAO,說白了就是封裝資料庫操作,連結跳轉了解:https://blog.csdn.net/zxy144/article/details/109279693

  • db:存放於資料庫相關的python類,操作資料表的程式碼
  • service:業務類程式碼
  • 核心程式碼:app.py

1.3.2.專案功能/需求描述

2.Code

2.1.db目錄

2.1.1.mysql_db.py

 1 import mysql.connector.pooling
 2 
 3 __config={
 4     "host":"localhost",
 5     "port":3306,
 6     "user":"root",
 7     "password":"123456",
 8     "database":"vega"
 9 }
10 
11 try:
12     pool=mysql.connector.pooling.MySQLConnectionPool(
13         **__config,
14         pool_size=10
15     )
16 except Exception as e:
17     print(e)

2.1.2.news_dao.py

  1 from db.mysql_db import pool
  2 
  3 class NewsDao:
  4     #查詢待審批新聞列表
  5     def search_unreview_list(self,page):
  6         try:
  7             con=pool.get_connection()
  8             cursor=con.cursor()
  9             sql="SELECT n.id,n.title,t.type,u.username " \
 10                 "FROM t_news n JOIN t_type t ON n.type_id=t.id " \
 11                 "JOIN t_user u ON n.editor_id=u.id " \
 12                 "WHERE n.state=%s " \
 13                 "ORDER BY n.create_time DESC " \
 14                 "LIMIT %s,%s"
 15             cursor.execute(sql,("待審批",(page-1)*10,10))
 16             result=cursor.fetchall()
 17             return result
 18         except Exception as e:
 19             print(e)
 20         finally:
 21             if "con" in dir():
 22                 con.close()
 23 
 24     # 查詢待審批新聞的總頁數
 25     def search_unreview_count_page(self):
 26         try:
 27             con=pool.get_connection()
 28             cursor=con.cursor()
 29             sql="SELECT CEIL(COUNT(*)/10) FROM t_news WHERE state=%s"
 30             cursor.execute(sql,["待審批"])
 31             count_page=cursor.fetchone()[0]
 32             return count_page
 33         except Exception as e:
 34             print(e)
 35         finally:
 36             if "con" in dir():
 37                 con.close()
 38 
 39  #審批新聞
 40     def update_unreview_news(self,id):
 41         try:
 42             con = pool.get_connection()
 43             con.start_transaction()
 44             cursor=con.cursor()
 45             sql="UPDATE t_news SET state=%s WHERE id=%s"
 46             cursor.execute(sql,("已審批",id))
 47             con.commit()
 48         except Exception as e:
 49             if "con" in dir():
 50                 con.rollback()
 51             print(e)
 52         finally:
 53             if "con" in dir():
 54                 con.close()
 55 
 56     #查詢新聞列表
 57     def search_list(self,page):
 58         try:
 59             con=pool.get_connection()
 60             cursor=con.cursor()
 61             sql="SELECT n.id,n.title,t.type,u.username " \
 62                 "FROM t_news n JOIN t_type t ON n.type_id=t.id " \
 63                 "JOIN t_user u ON n.editor_id=u.id " \
 64                 "ORDER BY n.create_time DESC " \
 65                 "LIMIT %s,%s"
 66             cursor.execute(sql,((page-1)*10,10))
 67             result=cursor.fetchall()
 68             return result
 69         except Exception as e:
 70             print(e)
 71         finally:
 72             if "con" in dir():
 73                 con.close()
 74 
 75     #查詢新聞總頁數
 76     def search_count_page(self):
 77         try:
 78             con=pool.get_connection()
 79             cursor=con.cursor()
 80             sql="SELECT CEIL(COUNT(*)/10) FROM t_news"
 81             cursor.execute(sql)
 82             count_page=cursor.fetchone()[0]
 83             return count_page
 84         except Exception as e:
 85             print(e)
 86         finally:
 87             if "con" in dir():
 88                 con.close()
 89 
 90     #刪除新聞
 91     def delete_by_id(self,id):
 92         try:
 93             con = pool.get_connection()
 94             con.start_transaction()
 95             cursor=con.cursor()
 96             sql="DELETE FROM t_news WHERE id=%s"
 97             cursor.execute(sql,[id])
 98             con.commit()
 99         except Exception as e:
100             if "con" in dir():
101                 con.rollback()
102             print(e)
103         finally:
104             if "con" in dir():
105                 con.close()

2.1.3.role_dao.py

from db.mysql_db import pool

class RoleDao:
    #查詢角色列表
    def search_list(self):
        try:
            con=pool.get_connection()
            cursor=con.cursor()
            sql="SELECT id,role FROM t_role"
            cursor.execute(sql)
            result=cursor.fetchall()
            return result
        except Exception as e:
            print(e)
        finally:
            if "con" in dir():
                con.close()

2.1.4.user_dao.py

  1 from db.mysql_db import pool
  2 
  3 class UserDao:
  4     #驗證使用者登入
  5     def login(self,username,password):
  6         try:
  7             con=pool.get_connection()
  8             cursor=con.cursor()
  9             sql="SELECT COUNT(*) FROM t_user WHERE username=%s AND " \
 10                 "AES_DECRYPT(UNHEX(password),'HelloWorld')=%s"
 11             cursor.execute(sql,(username,password))
 12             count=cursor.fetchone()[0]
 13             return True if count==1 else False
 14         except Exception as e:
 15             print(e)
 16         finally:
 17             if "con" in dir():
 18                 con.close()
 19 
 20     #查詢使用者角色
 21     def search_user_role(self,username):
 22         try:
 23             pass
 24             con=pool.get_connection()
 25             cursor=con.cursor()
 26             sql="SELECT r.role FROM t_user u JOIN t_role r ON u.role_id=r.id " \
 27                 "WHERE u.username=%s"
 28             cursor.execute(sql,[username])
 29             role=cursor.fetchone()[0]
 30             return role
 31         except Exception as e:
 32             print(e)
 33         finally:
 34             if "con" in dir():
 35                 con.close()
 36 
 37     #新增記錄
 38     def insert(self,username,password,email,role_id):
 39         try:
 40             con = pool.get_connection()
 41             con.start_transaction()
 42             cursor=con.cursor()
 43             sql="INSERT INTO t_user(username,password,email,role_id) " \
 44                 "VALUES(%s,HEX(AES_ENCRYPT(%s,'HelloWorld')),%s,%s)"
 45             cursor.execute(sql,(username,password,email,role_id))
 46             con.commit()
 47         except Exception as e:
 48             if "con" in dir():
 49                 con.rollback()
 50             print(e)
 51         finally:
 52             if "con" in dir():
 53                 con.close()
 54 
 55 
 56     #查詢使用者總頁數
 57     def search_count_page(self):
 58         try:
 59             con=pool.get_connection()
 60             cursor=con.cursor()
 61             sql="SELECT CEIL(COUNT(*)/10) FROM t_user"
 62             cursor.execute(sql)
 63             count_page=cursor.fetchone()[0]
 64             return count_page
 65         except Exception as e:
 66             print(e)
 67         finally:
 68             if "con" in dir():
 69                 con.close()
 70 
 71 #查詢使用者分頁記錄
 72     def search_list(self,page):
 73         try:
 74             con=pool.get_connection()
 75             cursor=con.cursor()
 76             sql="SELECT u.id,u.username,r.role " \
 77                 "FROM t_user u JOIN t_role r " \
 78                 "ON u.role_id=r.id " \
 79                 "ORDER BY u.id " \
 80                 "LIMIT %s,%s"
 81             cursor.execute(sql,((page-1)*10,10))
 82             result=cursor.fetchall()
 83             return result
 84         except Exception as e:
 85             print(e)
 86         finally:
 87             if "con" in dir():
 88                 con.close()
 89 
 90     #修改使用者資訊
 91     def update(self,id,username,password,email,role_id):
 92         try:
 93             con = pool.get_connection()
 94             con.start_transaction()
 95             cursor=con.cursor()
 96             sql="UPDATE t_user SET username=%s," \
 97                 "password=HEX(AES_ENCRYPT(%s,'HelloWorld'))," \
 98                 "email=%s,role_id=%s " \
 99                 "WHERE id=%s"
100             cursor.execute(sql,(username,password,email,role_id,id))
101             con.commit()
102         except Exception as e:
103             if "con" in dir():
104                 con.rollback()
105             print(e)
106         finally:
107             if "con" in dir():
108                 con.close()
109 
110     #刪除使用者
111     def delete_by_id(self,id):
112         try:
113             con = pool.get_connection()
114             con.start_transaction()
115             cursor=con.cursor()
116             sql="DELETE FROM t_user WHERE id=%s"
117             cursor.execute(sql,[id])
118             con.commit()
119         except Exception as e:
120             if "con" in dir():
121                 con.rollback()
122             print(e)
123         finally:
124             if "con" in dir():
125                 con.close()

2.2.service目錄

2.2.1.news_service.py

 1 from db.news_dao import NewsDao
 2 
 3 class NewsService:
 4     __news_dao=NewsDao()
 5 
 6     # 查詢待審批新聞列表
 7     def search_unreview_list(self,page):
 8         result=self.__news_dao.search_unreview_list(page)
 9         return result
10 
11     # 查詢待審批新聞的總頁數
12     def search_unreview_count_page(self):
13         count_page=self.__news_dao.search_unreview_count_page()
14         return count_page
15 
16     # 審批新聞
17     def update_unreview_news(self, id):
18         self.__news_dao.update_unreview_news(id)
19 
20     #查詢新聞列表
21     def search_list(self, page):
22         result=self.__news_dao.search_list(page)
23         return result
24 
25     # 查詢新聞總頁數
26     def search_count_page(self):
27         count_page=self.__news_dao.search_count_page()
28         return count_page
29 
30     # 刪除新聞
31     def delete_by_id(self, id):
32         self.__news_dao.delete_by_id(id)

2.2.2.role_service.py

1 from db.role_dao import RoleDao
2 
3 class RoleService:
4     __role_dao=RoleDao()
5 
6     # 查詢角色列表
7     def search_list(self):
8         result=self.__role_dao.search_list()
9         return result

2.2.3.user_service.py

 1 from db.user_dao import UserDao
 2 
 3 class UserService:
 4     __user_dao=UserDao()
 5 
 6     # 驗證使用者登入
 7     def login(self,username,password):
 8         result=self.__user_dao.login(username,password)
 9         return result
10 
11     # 查詢使用者角色
12     def search_user_role(self,username):
13         role=self.__user_dao.search_user_role(username)
14         return role
15 
16     # 新增記錄
17     def insert(self, username, password, email, role_id):
18         self.__user_dao.insert(username, password, email, role_id)
19 
20     # 查詢使用者總頁數
21     def search_count_page(self):
22         count_page = self.__user_dao.search_count_page()
23         return count_page
24 
25     # 查詢使用者分頁記錄
26     def search_list(self, page):
27         result = self.__user_dao.search_list(page)
28         return result
29 
30     # 修改使用者資訊
31     def update(self, id, username, password, email, role_id):
32         self.__user_dao.update(id, username, password, email, role_id)
33 
34     # 刪除使用者
35     def delete_by_id(self, id):
36         self.__user_dao.delete_by_id(id)

2.3.app.py啟動檔案

  1 from colorama import Fore,Style,init
  2 init()
  3 from getpass import getpass
  4 from service.user_service import UserService
  5 from service.news_service import NewsService
  6 from service.role_service import RoleService
  7 import os
  8 import sys
  9 import time
 10 
 11 
 12 __user_service=UserService()
 13 __news_service=NewsService()
 14 __role_service=RoleService()
 15 
 16 while True:
 17     os.system("cls")
 18     print(Fore.LIGHTBLUE_EX,"\n\t==================")
 19     print(Fore.LIGHTBLUE_EX,"\n\t歡迎使用新聞管理系統")
 20     print(Fore.LIGHTBLUE_EX, "\n\t==================")
 21     print(Fore.LIGHTGREEN_EX,"\n\t1.登陸系統")
 22     print(Fore.LIGHTGREEN_EX,"\n\t2.退出系統")
 23     print(Style.RESET_ALL)
 24     opt=input("\n\t輸入操作編號:")
 25     if opt=="1":
 26         username=input("\n\t使用者名稱:")
 27         password=getpass("\n\t密碼:")
 28         result=__user_service.login(username,password)
 29         #登陸成功
 30         if result==True:
 31             #查詢角色
 32             role=__user_service.search_user_role(username)
 33             while True:
 34                 os.system("cls")
 35                 if role=="新聞編輯":
 36                     print('test')
 37                 elif role=="管理員":
 38                     print(Fore.LIGHTGREEN_EX,"\n\t1.新聞管理")
 39                     print(Fore.LIGHTGREEN_EX, "\n\t2.使用者管理")
 40                     print(Fore.LIGHTRED_EX, "\n\tback.退出登陸")
 41                     print(Fore.LIGHTRED_EX, "\n\texit.退出系統")
 42                     print(Style.RESET_ALL)
 43                     opt = input("\n\t輸入操作編號:")
 44                     if opt=="1":
 45                         while True:
 46                             os.system("cls")
 47                             print(Fore.LIGHTGREEN_EX, "\n\t1.審批新聞")
 48                             print(Fore.LIGHTGREEN_EX, "\n\t2.刪除新聞")
 49                             print(Fore.LIGHTRED_EX, "\n\tback.返回上一層")
 50                             print(Style.RESET_ALL)
 51                             opt = input("\n\t輸入操作編號:")
 52                             if opt=="1":
 53                                 page=1
 54                                 while True:
 55                                     os.system("cls")
 56                                     count_page=__news_service.search_unreview_count_page()
 57                                     result=__news_service.search_unreview_list(page)
 58                                     for index in range(len(result)):
 59                                         one=result[index]
 60                                         print(Fore.LIGHTBLUE_EX, "\n\t%d\t%s\t%s\t%s"%(index+1,one[1],one[2],one[3]))
 61                                     print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
 62                                     print(Fore.LIGHTBLUE_EX,"\n\t%d/%d"%(page,count_page))
 63                                     print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
 64                                     print(Fore.LIGHTRED_EX, "\n\tback.返回上一層")
 65                                     print(Fore.LIGHTRED_EX, "\n\tprev.上一頁")
 66                                     print(Fore.LIGHTRED_EX, "\n\tnext.下一頁")
 67                                     print(Style.RESET_ALL)
 68                                     opt = input("\n\t輸入操作編號:")
 69                                     if opt=="back":
 70                                         break
 71                                     elif opt=="prev" and page>1:
 72                                         page-=1
 73                                     elif opt=="next" and page<count_page:
 74                                         page+=1
 75                                     elif int(opt)>=1 and int(opt)<=10:
 76                                         news_id=result[int(opt)-1][0]
 77                                         __news_service.update_unreview_news(news_id)
 78                             elif opt=="2":
 79                                 page=1
 80                                 while True:
 81                                     os.system("cls")
 82                                     count_page=__news_service.search_count_page()
 83                                     result=__news_service.search_list(page)
 84                                     for index in range(len(result)):
 85                                         one=result[index]
 86                                         print(Fore.LIGHTBLUE_EX, "\n\t%d\t%s\t%s\t%s"%(index+1,one[1],one[2],one[3]))
 87                                     print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
 88                                     print(Fore.LIGHTBLUE_EX,"\n\t%d/%d"%(page,count_page))
 89                                     print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
 90                                     print(Fore.LIGHTRED_EX, "\n\tback.返回上一層")
 91                                     print(Fore.LIGHTRED_EX, "\n\tprev.上一頁")
 92                                     print(Fore.LIGHTRED_EX, "\n\tnext.下一頁")
 93                                     print(Style.RESET_ALL)
 94                                     opt = input("\n\t輸入操作編號:")
 95                                     if opt=="back":
 96                                         break
 97                                     elif opt=="prev" and page>1:
 98                                         page-=1
 99                                     elif opt=="next" and page<count_page:
100                                         page+=1
101                                     elif int(opt)>=1 and int(opt)<=10:
102                                         news_id=result[int(opt)-1][0]
103                                         __news_service.delete_by_id(news_id)
104                             elif opt=="back":
105                                 break
106                     elif opt=="2":
107                         while True:
108                             os.system("cls")
109                             print(Fore.LIGHTGREEN_EX, "\n\t1.新增使用者")
110                             print(Fore.LIGHTGREEN_EX, "\n\t2.修改使用者")
111                             print(Fore.LIGHTGREEN_EX, "\n\t3.刪除使用者")
112                             print(Fore.LIGHTRED_EX, "\n\tback.返回上一層")
113                             print(Style.RESET_ALL)
114                             opt = input("\n\t輸入操作編號:")
115                             if opt=="back":
116                                 break
117                             elif opt=="1":
118                                 os.system("cls")
119                                 username=input("\n\t使用者名稱:")
120                                 password = getpass("\n\t密碼:")
121                                 repassword=getpass("\n\t重複密碼:")
122                                 if password!=repassword:
123                                     print("\n\t兩次密碼不一致(3秒自動返回)")
124                                     time.sleep(3)
125                                     continue
126                                 email=input("\n\t郵箱:")
127                                 result=__role_service.search_list()
128                                 for index in range(len(result)):
129                                     one=result[index]
130                                     print(Fore.LIGHTBLUE_EX,"\n\t%d.%s"%(index+1,one[1]))
131                                 print(Style.RESET_ALL)
132                                 opt=input("\n\t角色編號:")
133                                 role_id=result[int(opt)-1][0]
134                                 __user_service.insert(username,password,email,role_id)
135                                 print("\n\t儲存成功(3秒自動返回)")
136                                 time.sleep(3)
137                             elif opt=="2":
138                                 page = 1
139                                 while True:
140                                     os.system("cls")
141                                     count_page = __user_service.search_count_page()
142                                     result = __user_service.search_list(page)
143                                     for index in range(len(result)):
144                                         one = result[index]
145                                         print(Fore.LIGHTBLUE_EX,
146                                               "\n\t%d\t%s\t%s" % (index + 1, one[1], one[2]))
147                                     print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
148                                     print(Fore.LIGHTBLUE_EX, "\n\t%d/%d" % (page, count_page))
149                                     print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
150                                     print(Fore.LIGHTRED_EX, "\n\tback.返回上一層")
151                                     print(Fore.LIGHTRED_EX, "\n\tprev.上一頁")
152                                     print(Fore.LIGHTRED_EX, "\n\tnext.下一頁")
153                                     print(Style.RESET_ALL)
154                                     opt = input("\n\t輸入操作編號:")
155                                     if opt == "back":
156                                         break
157                                     elif opt == "prev" and page > 1:
158                                         page -= 1
159                                     elif opt == "next" and page < count_page:
160                                         page += 1
161                                     elif int(opt) >= 1 and int(opt) <= 10:
162                                         os.system("cls")
163                                         user_id=result[int(opt)-1][0]
164                                         username = input("\n\t新使用者名稱:")
165                                         password = getpass("\n\t新密碼:")
166                                         repassword = getpass("\n\t再次輸入密碼:")
167                                         if password!=repassword:
168                                             print(Fore.LIGHTRED_EX,"\n\t兩次密碼不一致(3秒自動返回)")
169                                             print(Style.RESET_ALL)
170                                             time.sleep(3)
171                                             break
172                                         email = input("\n\t新郵箱:")
173                                         result = __role_service.search_list()
174                                         for index in range(len(result)):
175                                             one = result[index]
176                                             print(Fore.LIGHTBLUE_EX, "\n\t%d.%s" % (index + 1, one[1]))
177                                         print(Style.RESET_ALL)
178                                         opt = input("\n\t角色編號:")
179                                         role_id = result[int(opt) - 1][0]
180                                         opt=input("\n\t是否儲存(Y/N)")
181                                         if opt=="Y" or opt=="y":
182                                             __user_service.update(user_id,username,password,email,role_id)
183                                             print("\n\t儲存成功(3秒自動返回)")
184                                             time.sleep(3)
185                             elif opt=="3":
186                                 page = 1
187                                 while True:
188                                     os.system("cls")
189                                     count_page = __user_service.search_count_page()
190                                     result = __user_service.search_list(page)
191                                     for index in range(len(result)):
192                                         one = result[index]
193                                         print(Fore.LIGHTBLUE_EX,
194                                               "\n\t%d\t%s\t%s" % (index + 1, one[1], one[2]))
195                                     print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
196                                     print(Fore.LIGHTBLUE_EX, "\n\t%d/%d" % (page, count_page))
197                                     print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
198                                     print(Fore.LIGHTRED_EX, "\n\tback.返回上一層")
199                                     print(Fore.LIGHTRED_EX, "\n\tprev.上一頁")
200                                     print(Fore.LIGHTRED_EX, "\n\tnext.下一頁")
201                                     print(Style.RESET_ALL)
202                                     opt = input("\n\t輸入操作編號:")
203                                     if opt == "back":
204                                         break
205                                     elif opt == "prev" and page > 1:
206                                         page -= 1
207                                     elif opt == "next" and page < count_page:
208                                         page += 1
209                                     elif int(opt) >= 1 and int(opt) <= 10:
210                                         os.system("cls")
211                                         user_id=result[int(opt)-1][0]
212                                         __user_service.delete_by_id(user_id)
213                                         print("\n\t刪除成功(3秒自動返回)")
214                                         time.sleep(3)
215 
216                     if opt=='back':
217                         break;
218                     elif opt=='exit':
219                         sys.exit(0)
220 
221         else:
222             print("\n\t登入失敗(3秒自動返回)")
223             time.sleep(3)
224 
225     elif opt=="2":
226         sys.exit(0)