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("程序执行完毕!")

吴裕雄 实战python编程(1)的更多相关文章

  1. 吴裕雄 实战PYTHON编程(10)

    import cv2 cv2.namedWindow("frame")cap = cv2.VideoCapture(0)while(cap.isOpened()): ret, im ...

  2. 吴裕雄 实战PYTHON编程(9)

    import cv2 cv2.namedWindow("ShowImage1")cv2.namedWindow("ShowImage2")image1 = cv ...

  3. 吴裕雄 实战PYTHON编程(8)

    import pandas as pd df = pd.DataFrame( {"林大明":[65,92,78,83,70], "陈聪明":[90,72,76, ...

  4. 吴裕雄 实战PYTHON编程(7)

    import os from win32com import client word = client.gencache.EnsureDispatch('Word.Application')word. ...

  5. 吴裕雄 实战PYTHON编程(6)

    import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['Simhei']plt.rcParams['axes.unicode ...

  6. 吴裕雄 实战PYTHON编程(5)

    text = '中华'print(type(text))#<class 'str'>text1 = text.encode('gbk')print(type(text1))#<cla ...

  7. 吴裕雄 实战PYTHON编程(4)

    import hashlib md5 = hashlib.md5()md5.update(b'Test String')print(md5.hexdigest()) import hashlib md ...

  8. 吴裕雄 实战python编程(3)

    import requests from bs4 import BeautifulSoup url = 'http://www.baidu.com'html = requests.get(url)sp ...

  9. 吴裕雄 实战python编程(2)

    from urllib.parse import urlparse url = 'http://www.pm25x.com/city/beijing.htm'o = urlparse(url)prin ...

随机推荐

  1. 如何使用Django 启动命令行及执行脚本

    使用django启动命令行和脚本,可以方便的使用django框架做开发,例如,数据库的操作等. 下面分别介绍使用方法. django shell的启动 启动命令: $/data/python-virt ...

  2. C# 5.0中引入了async 和 await

    C# 5.0中引入了async 和 await.这两个关键字可以让你更方便的写出异步代码. 看个例子: public class MyClass { public MyClass() { Displa ...

  3. 【EasyUI学习-2】Easyui Tree的异步加载

    作者:ssslinppp       1. 摘要 2. tree的相关介绍 3. 异步加载tree数据,并实现tree的折叠展开 3.1 功能说明: 3.2 前台代码 3.3 后台代码 4. 其他 1 ...

  4. bzoj1050 旅行

    Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求一条路径,使得路径上最大 ...

  5. linux中uptime命令查看linux系统负载

    阅读目录 uptime cat /proc/loadavg 何为系统负载呢? 进阶参考 uptime 另外还有一个参数 -V(大写),是用来查询版本的 [appdeploy@CNSZ22PL0088: ...

  6. VLC在web系统中应用(x-vlc-plugin 即如何把VLC嵌入HTML中)第一篇

    VLC毫无疑问是优秀的一款播放软件,子B/S机构的web项目中,如果能把它嵌入页面,做页面预览或者其他,是非常棒的. 第一步:下载VLC安装程序:(推荐1.0.3或者是1.0.5版本,比较稳定) ht ...

  7. python爬虫rp+bs4

    一.开发环境 Beautiful Soup 4.4.0 文档: http://beautifulsoup.readthedocs.io/zh_CN/latest/#id28 Requests : ht ...

  8. Spark分析之DAGScheduler

    DAGScheduler概述:是一个面向Stage层面的调度器: 主要入参有: dagScheduler.runJob(rdd, cleanedFunc, partitions, callSite, ...

  9. django的用户认证组件

    DataSource:https://www.cnblogs.com/yuanchenqi/articles/9064397.html 代码总结: 用户认证组件: 功能:用session记录登录验证状 ...

  10. Select算法(最坏复杂度O(n))

    #include<iostream> #include <stdio.h> #include <stdlib.h> #include <algorithm&g ...