Demo entry 6838901

Python3

   

Submitted by anonymous on Jun 12, 2019 at 05:46
Language: Python 3. Code size: 5.7 kB.

import pymysql
import getpass

db = pymysql.connect("localhost","root","root","sdb")    # 建立连接
if db != None:
    print("数据库连接成功")
else :
    print("数据库连接失败,请关闭程序重试。")
cursor = db.cursor()    # 设置游标,相当于命令行中的光标
sql_select = "SELECT %s FROM %s "
sql_update = "UPDATE %s SET %s"
sql_insert = "INSERT INTO %s VALUES %s"
sql_delete = "DELETE FROM %s %s"

def admin_mode():
    print("进入管理员模式。")
    while True:
        cmd = int(input("\n指令说明:0.退出;1.增加新电影数据;2.单独增加放映影院数据;3.清理无票影院数据或无票电影数据;4.下映某部电影。\n"))
        if cmd == 0:
            print("退出。\n")
            exit()
        elif cmd == 1:
            # UPDATE movie SET('Name', 'Abbreviation',Duration,'Company','Nationality')
            data = " (" + "'" + input("电影名称:") + "','" + input("简称:")  + "'," + input("时长:") + ",'" + input("制作公司:") + "','" + input("国籍:") + "')" 
            data = ('movie',) + (data,)
            cursor.execute(sql_insert % data)
            db.commit()
            data1 = '(' + "'" + input("电影信息新增完成,接下来添加影院信息\n简称:") + "','" + input("放映影院序号:") + "','" + input("价格:") + "','" + input("座位数:") +"')" 
            data1 = ('info',) + (data1,)
            cursor.execute(sql_insert % data1)
            db.commit()
            print("添加完成。")
        elif cmd == 2:
            data = '(' + "'" + input("添加影院信息\n简称:") + "','" + input("放映影院序号:") + "','" + input("价格:") + "','" + input("座位数:") +"')" 
            data = ('info',) + (data,)
            cursor.execute(sql_insert % data)
            db.commit()
            print("添加完成。")
        elif cmd == 3:
            # SELECT Abbreviation FROM info WHERE `Number of Unoccupied Seats`=0
            data = ('Abbreviation,`No.`','info WHERE `Number_of_Unoccupied_Seats`=0')
            cursor.execute(sql_select % data)
            results = cursor.fetchall()
            if results :    # 元组、列表为空直接为()和[],相当于False
                print("可删除信息:")
                for row in results:
                    print(f"无票电影名称:{row[0]},所在影院序号:{row[1]}\n检查本市是否全部售完...")
                    no = str(row[1])
                    data1 = 'WHERE Abbreviation=' + "'" +  row[0] + "'" + 'AND `No.`=' + no
                    data1 = ('info ', ) + (data1,)
                    cursor.execute(sql_delete % data1)
                    db.commit()
                    data2 = 'info WHERE Abbreviation=' + "'" + row[0] + "'"
                    data2 = ('*', ) + (data2,)
                    cursor.execute(sql_select % data2)
                    results1 =  cursor.fetchall()
                    if results1:
                        print("尚有影院在放映该电影。")
                    else :
                        print("本市该电影已无空座位,清理数据...")
                        data3 = 'WHERE ABbreviation=' +"'" + row[0] + "'"
                        data3 = ('movie ', ) + (data3,)
                        cursor.execute(sql_delete % data3)
                        db.commit()
            else :
                print("无符合条件数据。")
        elif cmd == 4:
            data = input("输入想要删除电影的简称:")
            data = 'WHERE Abbreviation=' + "'" + data + "'"
            data1 = ('info',) + (data,)
            cursor.execute(sql_delete % data1)
            db.commit()
            data2 = ('movie ', ) + (data,)
            cursor.execute(sql_delete % data2)
            db.commit()
            print("处理完成。")
        else :
            print("指令错误请重试!")

def normal_mode():
    print("进入用户模式。")
    while True:
        cmd = int(input("\n指令说明:0.退出;1.查询在映电影;2.查询有票影院及价格;3.订票(一次一张);4.退票(一次一张)\n"))
        if cmd == 0:
            print("退出。\n")
            exit()
        elif cmd == 1:
            data = ('*','movie')
            print("以下电影在映:")
            cursor.execute(sql_select % data)
            results = cursor.fetchall()
            if results :
                for row in results:
                    print(f"{row[0]}\t简称:{row[1]}")
            else :
                print("无符合条件数据。")
            print("")
        elif cmd == 2:
            data = 'cinema,info WHERE info.Abbreviation=' + "'" + input("\n输入想要查询的电影的简称,仅限一个:") + "'" + ' AND cinema.`No.`=info.`No.` AND `Number_of_Unoccupied_Seats` > 0 ORDER BY Prize ASC'
            data = ("cinema.Name_of_Cinema,cinema.`No.`,info.Prize",)+ (data,)
            cursor.execute(sql_select % data)
            results = cursor.fetchall()
            if results :
                for row in results:
                    print(f"影院名称:{row[0]}\t影院序号:{row[1]}\t价格{row[2]}")
            else:
                print("输入有误,或者票已售罄但管理员未清理数据。")
        elif cmd == 3:
            # UPDATE info SET `Number of Unoccupied Seats`=`Number of Unoccupied Seats`-1 WHERE Abbreviation='' 
            data =  ' `Number_of_Unoccupied_Seats`=`Number_of_Unoccupied_Seats`-1 WHERE Abbreviation=' + "'" + input("输入电影简称:") + "'" + 'AND  `No.`=' + input("输入影院序号:")
            data = ('info ', ) + (data,)
            cursor.execute(sql_update % data)
            db.commit()
            print("“咔哒咔哒”~出票成功谢谢使用!")
        elif cmd == 4:
            data =  ' `Number_of_Unoccupied_Seats`=`Number_of_Unoccupied_Seats`+1 WHERE Abbreviation=' + "'" + input("输入电影简称:") + "'" + 'AND  `No.`=' + input("输入影院序号:")
            data = ('info ', ) + (data,)
            cursor.execute(sql_update % data)
            db.commit()
            print("“咔哒咔哒”~退票成功谢谢使用!")
        else :
            print("指令错误请重试!")

# SELECT * FROM identity WHERE username='root' and pwd=md5('root');
ident = getpass.getpass("输入登录密码,正确则以管理员登录,否则为用户\n")
ident = 'identity WHERE username="root" AND pwd=MD5(' + "'" + ident + "'" + ')'
ident_data = ('*',) + (ident,)
cursor.execute(sql_select % ident_data)
result = cursor.fetchall()
if result:
    admin_mode()
else :
    normal_mode()
cursor.close()  # 这一句没做测试,如果报错就删这句话
db.close()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).