02 sqlite数据库应用(2)学生通讯录

1、数据库使用实例——学生通讯录

import sqlite3

打开数据库

def opendb():
    conn = sqlite3.connect('mydb.db')
    cur = conn.execute('''create table if not exists tongxunlu(usernum integer primary key, 
                                                               username varchar(128), 
                                                               password varchar(128),
                                                               address varchar(125), 
                                                               telnum varchar(128))''')
    return cur, conn

查询全部信息

def showalldb():
    print('----------------处理后的数据--------------------')
    hel = opendb()
    cur = hel[1].cursor()
    cur.execute('select * from tongxunlu')
    res = cur.fetchall()
    for line in res:
        for h in line:
            print(h, end = ',')
        print()
    cur.close()

输入信息

def into():
    usernum = input('请输入学号:')
    username1 = input('请输入姓名:')
    password1 = input('请输入密码:')
    address1 = input('请输入地址:')
    telnum1 = input('请输入联系电话:')
    return usernum, username1, password1, address1, telnum1

往数据库中添加内容

def adddb():
    welcome = "-------------欢迎使用添加数据功能------------------"
    print(welcome)
    person = into()
    hel = opendb()
    hel[1].execute("""insert into tongxunlu(usernum, username, password, address, telnum) values(?,?,?,?,?)""",
                  (person[0], person[1], person[2], person[3], person[4]))
    hel[1].commit()
    print('------------恭喜你,数据添加成功---------------')
    showalldb()
    hel[1].close()

删除数据库中的内容

def deldb():
    welcome = "-------------欢迎使用删除数据库功能------------------"
    print(welcome)
    delchoice = input('请输入想要删除学号:')
    hel = opendb()
    hel[1].execute("""delete from tongxunlu where usernum = """ + delchoice)
    hel[1].commit()
    print('------------恭喜你,数据删除成功---------------')
    showalldb()
    hel[1].close()

修改数据库的内容

def alter():
    welcome = "-------------欢迎使用修改数据库功能------------------"
    print(welcome)
    
    changechoice = input('请输入想要修改的学生的学号:')
    hel = opendb()
    person = into()
    hel[1].execute("""update tongxunlu set usernum=?,username=?,password=?,address=?,telnum=? where usernum = """ + 
                   changechoice,(person[0], person[1], person[2], person[3], person[4]))
    hel[1].commit()
    print('------------恭喜你,数据修改成功---------------')
    showalldb()
    hel[1].close()

查询数据

def searchdb():
    welcome = "-------------欢迎使用查询数据库功能------------------"
    print(welcome)
    choice = input('请输入要查询的学生的学号:')
    hel = opendb()
    cur = hel[1].cursor()
    cur.execute('select * from tongxunlu where usernum = ' + choice)
    hel[1].commit()
    print('------------恭喜你,你要查找的数据如下---------------')
    for row in cur:
        print(row[0], row[1], row[2], row[3], row[4])
    cur.close()
    hel[1].close()

是否继续

def conti():
    choice = input('是否继续?(y or n):')
    a = 1 if choice == 'y' else 0
    return a

主程序

if __name__ == '__main__':
    flag = 1
    while flag:
        welcome = '--------欢迎使用数据库通讯录---------'
        print(welcome)
        choiceshow = """
        请选择您的进一步选择:
        (添加)往数据库里面添加内容
        (删除)删除数据库中的内容
        (修改)修改数据库的内容
        (查询)查询数据库的内容
        选择您想要进行的操作:
        """
        choice = input(choiceshow)
        if choice == '添加':
            adddb()
            flag = conti()
        elif choice == '删除':
            deldb()
            flag = conti()
        elif choice == '修改':
            alter()
            flag = conti()
        elif choice == '查询':
            searchdb()
            flag = conti()
        else:
            print('你输入错误,请重新输入')

--------欢迎使用数据库通讯录---------

    请选择您的进一步选择:
    (添加)往数据库里面添加内容
    (删除)删除数据库中的内容
    (修改)修改数据库的内容
    (查询)查询数据库的内容
    选择您想要进行的操作:
    修改

-------------欢迎使用修改数据库功能------------------
请输入想要修改的学生的学号:1234
请输入学号:1084
请输入姓名:5463
请输入密码:5435
请输入地址:4154
请输入联系电话:54
------------恭喜你,数据修改成功---------------
----------------处理后的数据--------------------
1084,5463,5435,4154,54,
是否继续?(y or n):n

改进

1、当输入错误的时候,要提醒而不是报错

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值