1. 程式人生 > >模擬數據庫作業

模擬數據庫作業

rip break 循環 rgs split \n AS lose 函數

作業思路:

1,用戶選擇相應的選項進行選擇,1,註冊,2登陸,....(增刪改查)

2,使用裝飾器實現登錄認證

3,用戶id只能增加,刪除用戶之後id便不再被使用.

4,查:查詢遵循select 列名 where 條件/select * where 條件/select 列名 where phone like 133的模糊查詢.

  4.1實現思路:1,首先讓用戶輸入語法,首先進行判斷語法是否正確(確定用戶輸入的包含了select和where關鍵字).2,判斷列名是否存在name_list中,3,接下來就是切割用戶輸入的字符串,首先切割掉select,再切割where,得到用戶輸入的列名和條件,4,接下來操作文件循環文件,嵌套循環name_list再嵌套循環用戶輸入切割之後的列名列表.目的:最外層是為了最後輸出文件內容,嵌套的裏面兩層是進行用戶輸入的列名與所有列名進行匹配.進而判斷where後面的條件.(大體思路就這樣了.手動省略號......)

5,修改操作,與查詢開始的操作類似不在贅述,之後拿到用戶要修改的內容之後,將原數據切割之後賦值給變量,此時的變量代表的就是old的數據列表,進而匹配到相應的修改數據位置,把新的數據修改掉即可,最後將修改之後的列表轉化為逗號隔開的字符串寫入文件完成操作

***需要改進的地方***

再目前的代碼實現的僅僅只能判斷用戶一個條件.認為可以進行列名相同的修改操作達到判斷多條件的功能.未完,待續......

技術分享圖片
# ! /usr/bin/env python3.6
# -*- coding: utf-8 -*-
# 2018/6/21 20:05

‘‘‘
文件存儲格式如下:
id,name,age,phone,job
1,Alex,22,13651054608,IT
2,Egon,23,13304320533,Tearcher
3,nezha,25,1333235322,IT

現在需要對這個員工信息文件進行增刪改查。
基礎必做:
a.可以進行查詢,支持三種語法:
select 列名1,列名2,… where 列名條件
支持:大於小於等於,還要支持模糊查找。
示例:
select name, age where age>22
select * where job=IT
select * where phone like 133
進階選做:
b.可創建新員工記錄,id要順序增加c.可刪除指定員工記錄,直接輸入員工id即可
d.修改員工信息
語法:set 列名=“新的值” where 條件
#先用where查找對應人的信息,再使用set來修改列名對應的值為“新的值”

註意:要想操作員工信息表,必須先登錄,登陸認證需要用裝飾器完成
其他需求盡量用函數實現
‘‘‘ import os li = ["註冊", "登陸", 查詢, 修改, 添加, 刪除, 退出] dic = {"name": None} name_list = [id, name, age, phone, job] condition = [>, <, =, like] def register(): user_name = input(請輸入用戶名>>>) passwd = input(請輸入密碼>>>) with open(user_info
, r+, encoding=utf-8) as f: for item in f: if item.strip().split(,)[0] == user_name.strip(): print(您輸入的用戶名已存在,請重新輸入!) return else: f.write(user_name + , + passwd + \n) print(註冊成功!!!) def wrapper(func): def inner(*args, **kwargs): if dic[name] is None: print("請先登錄!!!") return ret = func(*args, **kwargs) return ret return inner def first_menu(): ‘‘‘選擇菜單‘‘‘ for num, item in enumerate(li): print(num + 1, item, end= ) def loggin(): print("歡迎登陸") flag = True while flag: username = input("請輸入您的用戶名>>>").strip() passwd = input("請輸入您的密碼>>>").strip() with open(user_info, encoding=utf-8) as f: for item in f.readlines(): name, pwd = item.strip().split(",") if name == username and passwd == pwd: dic[name] = username print(登陸成功!!!) flag = False break else: print(用戶名密碼輸入錯誤) @wrapper def add_info(): print("增加員工信息") flag1 = True while flag1: user_input = input("請輸入員工信息(姓名,年齡,電話,工作)>>>").strip() user_input = user_input.replace("", ,) with open("a.txt", r+, encoding=utf-8) as f: for item in f.readlines(): if item.strip().split(,)[1] == user_input.split(,)[0]: print(您輸入的用戶信息已存在) return else: with open("count_id", encoding=utf-8) as f1, open(count_id1, w, encoding=utf-8) as f2: count = f1.read() count = count.strip().split("=")[-1] f.write(count + , + user_input + \n) f2.write("count=" + str(int(count) + 1)) os.remove(count_id) os.rename(count_id1, count_id) @wrapper def reduce_info(): print("刪除員工信息") flag = True user_choice_id = input("請選擇你要刪除的員工id>>>") if user_choice_id.isdigit(): with open(a.txt, r, encoding=utf-8) as f, open(a1.txt, w, encoding=utf-8) as ff: for item in f: if item.strip().split(",")[0] == user_choice_id: flag = False print(刪除成功!!!) continue else: ff.write(item) else: while flag: print("沒有您要刪除的員工信息") break os.remove(a.txt) os.rename(a1.txt, a.txt) else: print(請輸入正確的員工id) @wrapper def select_info(): flag = False # with open(‘a.txt‘, encoding=‘utf-8‘) as f: # for item in f.readlines(): # print(item.strip()) # print("查找員工信息") user_choice = input(請輸入查找暗號>>>).strip() # select name, age where age>22 / select * where age>22 if select in user_choice and where in user_choice: lst = user_choice.split("select")[1:] # [‘ name, age where age>22‘] / lst1 = lst[0].split("where") # [‘ name, age ‘, ‘ age>22‘] lst1[1].strip() = ‘age>22‘ / [‘ *‘, ‘ age>22‘] lst_info = lst1[0].split(",") # [‘ name‘, ‘ age ‘] / [‘ *‘] for item in lst_info: if lst_info[0].strip() == "*": lst_info = name_list break elif item.strip() not in name_list: print("您輸入含有不存在列名,請重新選擇") return ‘‘‘這裏已經確定輸入的內容合法,下面打開文件進行查詢‘‘‘ with open(a.txt, r, encoding=utf-8) as f: for name in f: # 循環文件 if name.strip().split(,)[0] == id: for name in lst_info: print(name.strip(), end= ) print() continue for item in name_list: # item = ‘name‘ name_list = [‘id‘, ‘name‘, ‘age‘, ‘phone‘, ‘job‘] for el in lst_info: # [‘ name‘, ‘ age ‘] 或者只有一個[‘*‘] if item == el.strip(): for judje in condition: if judje in lst1[1].strip(): if condition.index(judje) == 0: # 確定是age大於操作 if int(name.strip().split(",")[ name_list.index(lst1[1].strip().split(judje)[0])]) > int( lst1[1].strip().split(judje)[-1]): print(name.strip().split(,)[name_list.index(el.strip())], end= ) flag = True elif condition.index(judje) == 1: ‘‘‘小於號操作‘‘‘ if int(name.strip().split(",")[ name_list.index(lst1[1].strip().split(judje)[0])]) < int( lst1[1].strip().split(judje)[-1]): print(name.strip().split(,)[name_list.index(el.strip())], end= ) flag = True elif condition.index(judje) == 2: ‘‘‘等於號操作,這裏兩種情況,1,id和age的大小。2,job的匹配‘‘‘ if lst1[1].strip().split(=)[0] == job: # ‘job = it‘ if name.strip().split(,)[ name_list.index(lst1[1].strip().split(judje)[0])].lower() == lst1[1].strip().split(judje)[-1].lower(): print(name.strip().split(,)[name_list.index(el.strip())], end= ) flag = True elif int(name.strip().split(",")[ name_list.index(lst1[1].strip().split(judje)[0])]) == int( lst1[1].strip().split(judje)[-1]): print(name.strip().split(,)[name_list.index(el.strip())], end= ) flag = True elif like in lst1[1].strip(): # lst1[1].strip() = ‘phone like 133‘ ‘‘‘模糊查詢‘‘‘ lst_phone = lst1[1].strip().split( like) # lst_phone = [‘phone ‘, ‘ 133‘] 註意:去空格 # print(lst_phone) if lst_phone[1].strip() in name.strip().split(,)[ name_list.index(lst_phone[0].strip())]: print(name.strip().split(,)[name_list.index(el.strip())], end= ) flag = True while flag: print() flag = False else: print(暗號語法錯誤,請重新選擇) # select name, age where age>22 # select * where job=IT # select * where phone like 133 @wrapper def modify_info(): ‘‘‘修改之前要先進行查找,根據語法條件,刪選出列名,新的值,where後面是舊的值‘‘‘ print("修改信息") flag = True # set 列名 =“新的值” where 條件 user_modify = input("請輸入修改的暗號>>>").strip() # set name = 超人 where name = 陳潤 if set in user_modify and where in user_modify: lst_user1 = user_modify.split(set)[1:][0].split(where) # lst_user1 = [‘name = 超人 ‘, ‘ name = 陳潤‘] old_value = lst_user1[1].strip() # ‘name = 陳潤‘ new_value = lst_user1[0].strip() # ‘name = 超人‘ with open(a.txt, r, encoding=utf-8) as f, open(a1.txt, w, encoding=utf-8) as ff: # ff.write(‘id,name,age,phone,job\n‘) for item in f: if item.strip().split(,)[name_list.index(old_value.split(=)[0].strip())] == old_value.split(=)[ 1].strip(): lst = item.strip().split(,) # [4,陳潤,23,17610780919,it] lst[name_list.index(old_value.split(=)[0].strip())] = new_value.split(=)[1].strip() s = ,.join(lst) ff.write(s + \n) print("修改成功!!!") flag = False else: ff.write(item) else: while flag: print(請輸入正確的信息) break os.remove(a.txt) os.rename(a1.txt, a.txt) else: print(您輸入的暗號有誤) def main(): while True: first_menu() user_input = input("請輸入您的選擇>>>").strip() if user_input == "1": register() elif user_input == "2": loggin() elif user_input == 3: select_info() elif user_input == 4: modify_info() elif user_input == 5: add_info() elif user_input == 6: reduce_info() elif user_input == 7: break else: print(請輸入正確的序號) if __name__ == __main__: main()
View Code

筆者水平有限,如有需要修改的地方,還望朋友指出,鄙人不勝感激!!!

模擬數據庫作業