python+mysql练手小项目(增删改查手机信息)

* mysql部分
我用的是db1数据库,并建立了两个表
在这里插入图片描述
phone表---------存储手机信息
在这里插入图片描述
user表---------存储账号密码
在这里插入图片描述
* python部分

1.连接数据库 (connection()函数)

#获取连接
def connection(sql,para):
    coon=pymysql.connect(host='localhost',
                     user='root',
                     password='*******',
                     port=3306,
                     db='db1',
                     charset='utf8')
    cur=coon.cursor()  #建立游标
    if para=="null":   
         cur.execute(sql) #查询数据
    else:
        cur.execute(sql,para)#增删改数据 ,para,代表的是sql的%s的值
    res=cur.fetchall() #,查找表中数据,返回结果是一个列表,
    coon.commit() #提交数据到数据库
    cur.close()  #关闭游标
    coon.close() #关闭连接
    return res

2.主函数(main()函数)

def main():
    while 1:
        print("*******************WELCOME*****************")
        print("*               1.登录                    *")
        print("*               2.注册                    *")
        print("*******************************************")
        choice=input("\n请输入您的选择:")
        if choice=='1':
            login()   #调用登录函数
            break
        if choice=='2':
            register()   #调用注册函数
        if choice!='1'and choice!='2':
            print("输入有误")
            continue
        

3.登录与注册(login()函数与register()函数)

#登录
def login():
    n=3
    while n>=0:
        number=input("请输入您的账号:")
        password=input("请您输入密码:")
        sql="select * from user where number=%s and password=%s"
        para=[number,password]
        res=connection(sql,para)    #因为connecton()函数返回的res是一个列表
        if len(res)==0:             #所以我用len(res)来判断里面有没有内容
            print("\n账号或密码输入错误,您还有"+str(n)+"次机会\n")
            n-=1
        else:
            print("\n\t\t\t登录成功\n")
            print1()      #调用菜单一

#注册    
def register():
    while 1:
        number=input("请输入账号")
        sql="select *from user where number=%s"
        para=[number]
        res=connection(sql,para)
        if(res!=0):
            print("\n此账号已存在\n")
        else:
            password=input("请输入密码")
            sql="insert into user(number,password) values(%s,%s)"
            para=[number,password]
            connection(sql,para)
            print("\n\t\t\t注册成功!")
            break

4.添加手机信息(add()函数)

#添加手机信息函数
def add():
    name=input("请输入手机名称:")
    price=input("请输入手机价钱:")
    typee=input("请输入手机类型:")
    sql="insert into phone (name,money,type) values(%s,%s,%s)"
    para=[name,price,typee]
    connection(sql,para)
    sql="select *from phone where name=%s"  #添加信息后在查询一下有没有加入到数据库
    para=[name]
    res=connection(sql,para)
    if len(res)!=0:   #len(res)!=0说明查询到,添加成功
        print("添加成功")
    else:
        print("添加失败")

5.删除手机信息(delete()函数 用到了删除id之后,id怎么重新排序)

def delete():
    show()  #删除之前先查看一下所有的手机
    choice=input("请输入你要删除的编号:")
    sql="delete from phone where id=%s"
    para=[choice]
    connection(sql,para)
    sql="select *from phone where id=%s"     #删除信息后查看一下还能不能在查到这个信息
    para=[choice]
    res=connection(sql,para)
    if len(res)!=0:
        print("删除失败")
    else:
        print("删除成功")
    #重新更新手机编号(解决删除之后编号不连续)
    sql="alter table phone drop id"    #删除原有主键
    connection(sql,"null")
    sql="alter table phone add id int not null first"#添加新主键字段
    connection(sql,"null")
    sql="alter table phone modify column id int not null auto_increment,add primary key(id)" #设置新主键
    connection(sql,"null")

6.修改手机信息(update()函数 分为三部分具体修改)

#修改手机信息               
def update():
    show()
    global cho    #我在这里设置了一个全局变量,在修改具体信息时用
    cho=input("请输入您要修改的编号:")
    sql="select *from phone where id=%s"
    para=[cho]
    res=connection(sql,para)
    if(len(res)!=0):  #如果编号存在调用函数print2
        print2()
    else:
        print("查无此编号,请检查输入是否正确")

6.(1)修改手机名称(updateName()函数)

def updateName():
    name=input("请输入您修改后的手机名称:")
    sql="update phone set name=%s where id=%s"
    para=[name,cho]    #这里的cho是update里要修改的编号
    res=connection(sql,para)
    if(len(res)==0):
        print("修改成功!")
    else:
        print("修改失败!")

6.(2)修改手机价钱(updateMoney()函数)

def updateMoney():
    money=input("请输入您的修改后的手机价格:")
    sql="update phone set money=%s where id=%s"
    para=[money,cho]       #这里的cho是update里要修改的编号
    res=connection(sql,para)
    if(len(res)==0):
        print("修改成功!")
    else:
        print("修改失败!")

6.(3)修改手机类型(updateType()函数)

def updateType():
    typee=input("请输入您修改后的手机类型:")
    sql="update phone set type=%s where id=%s"
    para=[typee,cho]       #这里的cho是update里要修改的编号
    res=connection(sql,para)
    if(len(res)==0):
        print("修改成功!")
    else:
        print("修改失败!")

7.查看手机信息(show()函数 分为三部分具体修改)

def show():
    sql="select *from phone"
    res=connection(sql,"null")
    tlt="{:>4}\t{:<8}\t{:<10}\t{:<10}\t"  #输出格式
    print(tlt.format("编号","手机名称","价钱","手机类型"))
    for i in res:           #res是一个列表
        print(tlt.format(i[0],i[1],i[2],i[3])) 
        

8.菜单1(print1()函数 )

def print1():
    while 1:
        print("\n")
        print("*******************WELCOME*****************")
        print("*               1.增加手机信息            *")
        print("*               2.删除手机信息            *")
        print("*               3.修改手机信息            *")
        print("*               4.查看手机信息            *")
        print("*               0.退出                    *")
        print("*******************************************")
        choice=input("请输入您的选择:")
        if choice=='1':
            add()
        if choice=='2':
            delete()
        if choice=='3':
            update()
        if choice=='4':
            show()
        if choice=='0':
            print("\n\n\t感谢您的使用!")
            sys.exit(0)         #退出此程序,需要导入sys模块

9.菜单2(print2()函数,修改手机信息子菜单 )

def print2():
    word='y'
    while word=='y':     
        print("\n")
        print("*******************WELCOME*****************")
        print("*                 1.修改名称              *")
        print("*                 2.修改价钱              *")
        print("*                 3.修改类型              *")
        print("*                 4.返回上一级            *")
        print("*******************************************")
        choice=input("请输入您的选择:")
        if choice=="1":
            updateName()
        if choice=="2":
            updateMoney()
        if choice=="3":
            updateType()
        if choice=="4":
            print1()
        word=input("是否还继续 y/n:")

10.调用主函数

main()

* 结果展示
结果我就不展示了,已测可行 # _ # 哈哈哈哈

发布了9 篇原创文章 · 获赞 23 · 访问量 1192

猜你喜欢

转载自blog.csdn.net/weixin_43920952/article/details/104213545
今日推荐