吴裕雄 实战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("程序执行完毕!")


吴裕雄 实战python编程(1)的更多相关文章
- 吴裕雄 实战PYTHON编程(10)
import cv2 cv2.namedWindow("frame")cap = cv2.VideoCapture(0)while(cap.isOpened()): ret, im ...
- 吴裕雄 实战PYTHON编程(9)
import cv2 cv2.namedWindow("ShowImage1")cv2.namedWindow("ShowImage2")image1 = cv ...
- 吴裕雄 实战PYTHON编程(8)
import pandas as pd df = pd.DataFrame( {"林大明":[65,92,78,83,70], "陈聪明":[90,72,76, ...
- 吴裕雄 实战PYTHON编程(7)
import os from win32com import client word = client.gencache.EnsureDispatch('Word.Application')word. ...
- 吴裕雄 实战PYTHON编程(6)
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['Simhei']plt.rcParams['axes.unicode ...
- 吴裕雄 实战PYTHON编程(5)
text = '中华'print(type(text))#<class 'str'>text1 = text.encode('gbk')print(type(text1))#<cla ...
- 吴裕雄 实战PYTHON编程(4)
import hashlib md5 = hashlib.md5()md5.update(b'Test String')print(md5.hexdigest()) import hashlib md ...
- 吴裕雄 实战python编程(3)
import requests from bs4 import BeautifulSoup url = 'http://www.baidu.com'html = requests.get(url)sp ...
- 吴裕雄 实战python编程(2)
from urllib.parse import urlparse url = 'http://www.pm25x.com/city/beijing.htm'o = urlparse(url)prin ...
随机推荐
- Cenots7对lvm逻辑卷分区大小的调整
Cenots7对lvm逻辑卷分区大小的调整 (针对xfs和ext4不同文件系统) 1.支持的文件系统类型 特别注意的是: resize2fs命令 针对的是ext2.ext3.ex ...
- python中的with
看例 """ 需求:不用数据库连接池,实现数据库链接操作 """ class SQLHelper(object): def open(sel ...
- ExtJS中,将Grid表头中的全选复选框取消复选
今天发现公司产品用的EXTJS中使用Grid时,Grid表头中的全选复选框的选中状态不是很准确,就写了这个小扩展 在js中加入下面方法,在需要取消全选的地方调用即可,例:Ext.getCmp('gri ...
- 关于pycharm导入其他项目时出现找不到python无法运行的问题
之前拿到一个别的人用scrapy写的一个爬虫想运行看看,然后就出了类似于这种错误(类似的,这个是网上找的),一直提示找不到XXX路径下的python,然后无法运行执行文件... 我一看这个简单,这种就 ...
- 使用xmlHttprequest有感
原文地址:http://my.oschina.net/LinBandit/blog/33160 之前一片日志说使用xmlhttprequest获取服务数据时,在IE下能通过而在chrome不能通过的问 ...
- Nginx Tengine ngx_http_upstream_check_module 健康功能检测使用
该模块可以为Tengine提供主动式后端服务器健康检查的功能. 该模块在Tengine-1.4.0版本以前没有默认开启,它可以在配置编译选项的时候开启:./configure --with-http_ ...
- CA证书扫盲,https讲解
很多关于CA证书的讲解. 1.什么是CA证书. 看过一些博客,写的比较形象具体. ◇ 普通的介绍信 想必大伙儿都听说过介绍信的例子吧?假设 A 公司的张三先生要到 B 公司去拜访,但是 B 公司的所有 ...
- javascript节点操作移出节点removeChild()
removeChild(a)是用来删除文档中的已有元素 参数a:要移出的节点 <div id="guoDiv"> <span>1</span> ...
- HFDS核心技术
HDFS 设计的前提与目标 HDFS体系结构1 HDFS体系结构2 HDFS特性与优点 高容错性保障机制 HDFS不适合的场景 HDFS2.0的新特征 HA-QJM Federation 快照 异构层 ...
- 香侬科技独家对话Facebook人工智能研究院首席科学家Devi Parikh
Facebook 人工智能研究院(FAIR)首席科学家 Devi Parikh 是 2017 年 IJCAI 计算机和思想奖获得者(IJCAI 两个最重要的奖项之一,被誉为国际人工智能领域的「菲尔兹奖 ...