吳裕雄 實戰python程式設計(1)
import sqlite3
conn = sqlite3.connect('E:\\test.sqlite') # 建立資料庫聯接
cursor = conn.cursor() # 建立 cursor 物件
#新建一個數據表
sqlstr='CREATE TABLE IF NOT EXISTS table01 ("num" INTEGER PRIMARY KEY NOT NULL ,"tel" TEXT)'
cursor.execute(sqlstr)
# 新增一條記錄
sqlstr='insert into table01 values(1,"02-1234567")'
cursor.execute(sqlstr)
conn.commit() # 主動更新
conn.close() # 關閉資料庫連線
def menu():
os.system("cls")
print("賬號、密碼管理系統")
print("-------------------------")
print("1. 輸入賬號、密碼")
print("2. 顯示賬號、密碼")
print("3. 修 改 密 碼")
print("4. 刪除賬號、密碼")
print("0. 結 束 程 序")
print("-------------------------")
def ReadData():
with open('E:\\password.txt','r', encoding = 'UTF-8-sig') as f:
filedata = f.read()
if filedata != "":
data = ast.literal_eval(filedata)
return data
else: return dict()
def disp_data():
print("賬號\t密碼")
print("================")
for key in data:
print("{}\t{}".format(key,data[key]))
input("按任意鍵返回主選單")
def input_data():
while True:
name =input("請輸入賬號(Enter==>停止輸入)")
if name=="": break
if name in data:
print("{}賬號已存在!".format(name))
continue
password=input("請輸入密碼:")
data[name]=password
with open('password.txt','w',encoding = 'UTF-8-sig') as f:
f.write(str(data))
print("{}已儲存完畢".format(name))
def edit_data():
while True:
name =input("請輸入要修改的賬號(Enter==>停止輸入)")
if name=="": break
if not name in data:
print("{} 賬號不存在!".format(name))
continue
print("原密碼為:{}".format(data[name]))
password=input("請輸入新密碼:")
data[name]=password
with open('password.txt','w',encoding = 'UTF-8-sig') as f:
f.write(str(data))
input("密碼更改完畢,請按任意鍵返回主選單")
break
def delete_data():
while True:
name =input("請輸入要刪除的賬號(Enter==>停止輸入)")
if name=="": break
if not name in data:
print("{} 賬號不存在!".format(name))
continue
print("確定刪除{}的資料!:".format(name))
yn=input("(Y/N)?")
if (yn=="Y" or yn=="y"):
del data[name]
with open('password.txt','w',encoding = 'UTF-8-sig') as f:
f.write(str(data))
input("已刪除完畢,請按任意鍵返回主選單")
break
### 主程式從這裡開始 ###
import os,ast
data=dict()
data = ReadData() # 讀取文字檔案後轉換為 dict
while True:
menu()
choice = int(input("請輸入您的選擇:"))
print()
if choice==1:
input_data()
elif choice==2:
disp_data()
elif choice==3:
edit_data()
elif choice==4:
delete_data()
else:
break
print("程式執行完畢!")
import sqlite3
def menu():
os.system("cls")
print("賬號、密碼管理系統")
print("-------------------------")
print("1. 輸入賬號、密碼")
print("2. 顯示賬號、密碼")
print("3. 修 改 密 碼")
print("4. 刪除賬號、密碼")
print("0. 結 束 程 序")
print("-------------------------")
def disp_data():
cursor = conn.execute('select * from password')
print("賬號\t密碼")
print("================")
for row in cursor:
print("{}\t{}".format(row[0],row[1]))
input("按任意鍵返回主選單")
def input_data():
while True:
name =input("請輸入賬號(Enter==>停止輸入)")
if name=="": break
sqlstr="select * from password where name='{}'" .format(name)
cursor=conn.execute(sqlstr)
row = cursor.fetchone()
if not row==None:
print("{} 賬號已存在!".format(name))
continue
password=input("請輸入密碼:")
sqlstr="insert into password values('{}','{}');".format(name,password)
conn.execute(sqlstr)
conn.commit()
print("{} 已儲存完畢".format(name))
def edit_data():
while True:
name =input("請輸入要修改的賬號(Enter==>停止輸入)")
if name=="": break
sqlstr="select * from password where name='{}'" .format(name)
cursor=conn.execute(sqlstr)
row = cursor.fetchone()
print(row)
if row==None:
print("{} 賬號不存在!".format(name))
continue
print("原來密碼為:{}".format(row[1]))
password=input("請輸入新密碼:")
sqlstr = "update password set pass='{}' where name='{}'".format(password, name)
conn.execute(sqlstr)
conn.commit()
input("密碼更改完畢,請按任意鍵返回主選單")
break
def delete_data():
while True:
name =input("請輸入要刪除的賬號(Enter==>停止輸入)")
if name=="": break
sqlstr="select * from password where name='{}'" .format(name)
cursor=conn.execute(sqlstr)
row = cursor.fetchone()
if row==None:
print("{} 賬號不存在!".format(name))
continue
print("確定刪除{}的資料!:".format(name))
yn=input("(Y/N)?")
if (yn=="Y" or yn=="y"):
sqlstr = "delete from password where name='{}'".format(name)
conn.execute(sqlstr)
conn.commit()
input("已刪除完畢,請按任意鍵返回主選單")
break
### 主程式從這裡開始 ###
import os,sqlite3
conn = sqlite3.connect('E:\\Sqlite01.sqlite')
while True:
menu()
choice = int(input("請輸入您的選擇:"))
print()
if choice==1:
input_data()
elif choice==2:
disp_data()
elif choice==3:
edit_data()
elif choice==4:
delete_data()
else:
break
conn.close()
print("程式執行完畢!")