码迷,mamicode.com
首页 > 其他好文 > 详细

员工增删改查

时间:2016-12-12 02:24:43      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:open   lag   strip   删除   增删改   input   pen   查询   inf   

#! /usr/bin/env python
# -*- coding:utf-8 -*-
import json
item_list = [
    "1.query",
    "2.add",
    "3.del",
    "4.modify",
]
yg_dic = {
"1": {"name": "alex", "age": 22, "phone": "15122000000", "dept": "IT", "enroll_date": "2013-04-11"},
"2": {"name": "leif", "age": 23, "phone": "15122000001", "dept": "IT", "enroll_date": "2013-04-12"},
"3": {"name": "alex1", "age": 24, "phone": "15122000002", "dept": "IT", "enroll_date": "2013-04-13"},
"4": {"name": "alex2", "age": 25, "phone": "15122000003", "dept": "IT", "enroll_date": "2013-04-14"},
"5": {"name": "alex3", "age": 26, "phone": "15122000004", "dept": "IT", "enroll_date": "2013-04-15"},
}
# yg_dic["01"]["dept"] = "ii"
# print(yg_dic)
# with open("data.json", "w", encoding="UTF-8") as f_dump:
#     s_dump = json.dump(yg_dic, f_dump, ensure_ascii=False)
# with open("data.json", "r", encoding="UTF-8") as f_load:
#     r_load = json.load(f_load)
# print(r_load)

# col = "name"
# print(yg_dic["01"][col])

# with open("data.json", "r", encoding="UTF-8") as f_load:
#     r_load = json.load(f_load)
# print(len(yg_dic))
# colume = "age"
# print(colume)
# for key,valus in yg_dic.items():
#     # print(key)
#     print(valus)
#     print(valus[colume])
# print(emp_info["05"]["age"])


def main():
    quit_flag = False
    while not quit_flag:
        print(item_list)
        choice = input("make your choice:")
        if choice == "1":
            sql = input("sql语句:").strip()
            query(sql)
        elif choice == "2":
            add()
        elif choice == "3":
            dele()
        elif choice == "4":
            sql = input("sql语句:").strip()
            modify(sql)
        elif choice == "q" or choice == "quit":
            quit_flag = True
            exit("quit")
        else:
            print("invalid type")


def query(sql):  # 查询
    sql_list = sql.split("where")
    print(sql_list[0],len(sql_list))
    if sql_list[0].startswith("select") and len(sql_list) > 1:
        if ">" in sql_list[1]:
            column, val = sql_list[1].strip().split(">")
            record = []
            for key,valus in yg_dic.items():
                if int(valus[column.strip()]) > int(val):
                    print(key, valus)
                    record.append(key)
            print("查询到的条数: %s" % len(record))
        elif "<" in sql_list[1]:
            column, val = sql_list[1].strip().split("<")
            record = []
            for key, valus in yg_dic.items():
                if int(valus[column.strip()]) < int(val):
                    print(key, valus)
                    record.append(key)
            print("查询到的条数: %s" % len(record))
        elif "=" in sql_list[1]:
            column, val = sql_list[1].strip().split("=")
            record = []
            for key, valus in yg_dic.items():
                if int(valus[column.strip()]) == int(val):
                    print(key, valus)
                    record.append(key)
            print("查询到的条数: %s" % len(record))
        elif "like" in sql_list[1]:
            column, val = sql_list[1].strip().split("like")
            record = []
            for key, valus in yg_dic.items():
                if val in valus:
                    print(key, valus)
                    record.append(key)
            print("查询到的条数: %s" % len(record))


def add():
    nums = len(yg_dic) + 1
    contest = input("员工信息:").strip()
    yg_dic[nums] = contest
    print(yg_dic)


def dele():  # 可删除指定员工信息纪录,输入员工id,即可删除
    id_choice = input("输入员工id").strip()
    yg_dic.pop(id_choice)
    print(yg_dic)


def modify(sql):
    sql_list = sql.split("where")
    print(sql_list[0], len(sql_list))
    if sql_list[0].startswith("update") and len(sql_list) > 1:
        column, val = sql_list[1].strip().split(=)
        modify_val = sql.strip().split("\"")[1]
        for key, valus in yg_dic.items():
            yg_dic[key][column.strip()] = modify_val
        print(yg_dic)

# sql = "select name,age from staff_table where age > 22"
# select * from staff_table where dept = "IT"
# select * from staff_table where enroll_date like "2013"
# update staff_table SET dept="Market" where dept = "IT"
if __name__ == __main__:
    main()

 

员工增删改查

标签:open   lag   strip   删除   增删改   input   pen   查询   inf   

原文地址:http://www.cnblogs.com/lifei-python/p/6161134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!