吴裕雄 实战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("程序执行完毕!")
最新文章
- CSS3文本温故
- C#连接数据库的新方法(通过web.config配置文件)
- 转:判断DATASET是否为空
- [LeetCode] Letter Combinations of a Phone Number(bfs)
- intel simd 资料
- WPF之给使用了模板的MenuItem添加快捷操作
- 【转】MTK Android Driver知识大全
- Transition 所支持的css属性
- 101个Linq例子(40-60)
- 不高兴的o( ̄ヘ ̄o#)JJ
- (四)五种IO模型
- Linux--奇思淫才
- HotSpot虚拟机对象探秘(对象创建,对象内存布局,对象访问定位)
- Nintex Workflow Get Attachment link
- elasticsearch数据结构
- centos6.6安装hadoop-2.5.0(六、各种node功能)
- Java调用Jenkins接口实现远程发版
- 【FusionCharts学习-1】获取资源
- MapReduce(五) mapreduce的shuffle机制 与 Yarn
- 北航软院2015级C#期末考试部分考题讲解
热门文章
- [C++ Primer] : 第15章: 面向对象程序设计
- centOS 6.5关闭防火墙步骤
- VS2010中visual assist x的一些问题
- 安全测试chicklist
- Android logcat命令详解
- [UE4]GetWorld()->;GetDeltaSeconds()方法
- pandas的map函数与apply函数的区别
- 向Nexus仓库推送/使用各种组件
- MySQL ALTER讲解
- BigDecimal空指针异常——个人应用