• 吴裕雄 实战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("账号 密码")
    print("================")
    for key in data:
    print("{} {}".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("账号 密码")
    print("================")
    for row in cursor:
    print("{} {}".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("程序执行完毕!")

  • 相关阅读:
    答题卡
    hdu 5451 Best Solver
    L. Poor God Water(ACM-ICPC 2018 焦作赛区网络预赛)
    MicroRNA Ranking(Tehran2016)
    Split The Tree(2018东北四省赛)
    Django项目基础开发流程
    暑假学习进度记录墙
    抖音字体设置
    十大危险cmd指令
    奶牛的聚会
  • 原文地址:https://www.cnblogs.com/tszr/p/10061103.html
Copyright © 2020-2023  润新知