1. 程式人生 > >python操作資料庫,實現使用者名稱、密碼登入資料庫,首次登入自行設定密碼,並返回工資表明細。

python操作資料庫,實現使用者名稱、密碼登入資料庫,首次登入自行設定密碼,並返回工資表明細。

python操作資料庫,實現使用者名稱、密碼登入資料庫,首次登入自行設定密碼,並返回工資表明細。

 1 #!/usr/bin/env python3
 2 # -*- coding: utf-8 -*-
 3 
 4 # 匯入依賴包
 5 import psycopg2
 6 
 7 print("營口菸草工資查詢系統。授權遼寧省菸草公司營口市公司人事科使用。")
 8 print("注意!!!,本系統僅供查詢當月工資,每月8日更新資料,8日後可查詢當月工資。")
 9 print("開發者:CoolDSJ")
10 print()
11 
12 
13 def showdate(username):
14 """讀取資料庫工資條資訊,根據使用者名稱讀取不同的記錄。""" 15 # conn = psycopg2.connect(database="shop", user="postgres", password="*******", host="localhost", port="5432") 16 conn = psycopg2.connect(database="postgres", user="postgres", password="********", host="********9", port="5432") 17 cur = conn.cursor()
18 cur.execute("select * from gongzi where ygbh='%s'" % username) 19 results = cur.fetchone() 20 cur.close() 21 conn.close() 22 return results 23 24 25 def gaimm(new_mima,username): 26 """第一次使用的使用者,提醒更改密碼,將新密碼存入資料庫的使用者名稱對應密碼列中。""" 27 # conn = psycopg2.connect(database="shop", user="postgres", password="*******", host="localhost", port="5432")
28 conn = psycopg2.connect(database="postgres", user="postgres", password="*******", host="********", port="5432") 29 cur = conn.cursor() 30 cur.execute("update gongzi set mm ='%s' where ygbh='%s'"%(new_mima,username)) #新密碼寫入資料庫對應列。 31 conn.commit() #修改資料庫後,必須用該語句提交修改。 32 cur.close() 33 conn.close() 34 35 36 def val(): 37 name = input('請輸入使用者名稱,並按回車鍵。>>>>>>') # 輸入使用者名稱 38 results = showdate(name) # 獲取使用者名稱對應的資料庫資料 39 if results: 40 mima = input('請輸入密碼,並按回車鍵。>>>>>>') # 輸入使用者名稱 41 if str(results[11]) == 'nnnn': 42 mima = input('您是第一次使用系統,請設定一個4位數字密碼,並按回車鍵。>>>>>>') 43 if len(mima) != 4: 44 print("請輸入4位數字!") 45 mima = input('請設定一個4位數字密碼,並按回車鍵。>>>>>>') 46 if len(mima) == 4 : 47 xiugai = gaimm(mima, name) #呼叫寫入資料庫密碼函式。 48 print("密碼修改成功。") 49 val() 50 else: 51 print("未能成功修改密碼,請重新操作!!!。") 52 val() 53 else: 54 xiugai = gaimm(mima, name) #呼叫寫入資料庫密碼函式。 55 print("密碼修改成功。") 56 val() 57 elif results[11] == mima: 58 print() 59 print(results[1], "同志,您好:") 60 print(" 您2018年11月份工資明細如下:") 61 print() 62 print(" 專案名稱 專案內容") 63 print(" ---------- ------------") 64 print("‖姓 名‖", results[0]) 65 print("‖姓 名‖", results[0]) 66 print("‖崗位工資‖", results[1]) 67 print("‖績效工資‖", results[2]) 68 print("‖績效考評‖", results[3]) 69 print("‖工資合計‖", results[4]) 70 print("‖公 積 金‖", results[5]) 71 print("‖養老保險‖", results[6]) 72 print("‖醫療保險‖", results[7]) 73 print("‖失業保險‖", results[8]) 74 print("‖所 得 稅‖", results[9]) 75 print("‖企業年金‖", results[10]) 76 print("‖實發工資‖", results[11]) 77 print() 78 print("以上工資,如與實際不符,或有其它疑問,請及時與市局(公司)人事科部溝通。") 79 print("祝工作順利,身體健康。") 80 else: 81 print("密碼錯誤") 82 else: 83 print("使用者名稱錯誤") 84 85 86 val() 87 input()