#作業說明 現要求你寫一個簡單的員工信息增刪改查程序 文件存儲時可以這樣表示 1,Alex Li,22,13651054608,IT,2013-04-01 2,Jack Wang,28,13451024608,HR,2015-01-07 3,Rain Wang,21,13451054608,IT,2017-04-01 4,Mack Qiao,44,15653354208,Sales,2016-02-01 5,Rachel Chen,23,13351024606,IT,2013-03-16 6,Eric Liu,19,18531054602,Marketing,2012-12-01 7,Chao Zhang,21,13235324334,Administration,2011-08-08 8,Kevin Chen,22,13151054603,Sales,2013-04-01 9,Shit Wen,20,13351024602,IT,2017-07-03 10,Shanshan Du,26,13698424612,Operation,2017-07-02 1.可進行模糊查詢,語法至少支持下面3種查詢語法: find name,age from staff_table where age > 22 find * from staff_table where dept = "IT" find * from staff_table where enroll_date like "2013" 2.可創建新員工紀錄,以phone做唯一鍵(即不允許表里有手機號重復的情況),staff_id需自增 語法: add staff_table Alex Li,25,134435344,IT,2015-10-29 3.可刪除指定員工信息紀錄,輸入員工id,即可刪除 語法: del from staff where id=3 4.可修改員工信息,語法如下: UPDATE staff_table SET dept="Market" WHERE dept = "IT" 把所有dept=IT的紀錄的dept改成Market UPDATE staff_table SET age=25 WHERE name = "Alex Li" 把name=Alex Li的紀錄的年齡改成25 5.以上每條語名執行完畢后,要顯示這條語句影響了多少條紀錄。 比如查詢語句 就顯示 查詢出了多少條、修改語句就顯示修改了多少條等。 ##目錄說明 leco@leco:~/luffy/module_1/user_info$ tree . . ├── user_info.py # 主程序 ├── data # 數據庫文夾(首次運行后會初始化創造該文件夾) │ └── staff_table.db # 數據庫文件(首次運行后會初始化創造該文件) ├── readme.md # 程序說明文件 └── 人員信息增刪改查流程圖.bmp # 流程圖 ## 運行環境 Python 3.5.2
## 基礎環境包安裝
cmz@leco:~/luffy/mod$ pip install tabulate
Collecting tabulate
Downloading tabulate-0.8.2.tar.gz (45kB)
100% |████████████████████████████████| 51kB 97kB/s
Building wheels for collected packages: tabulate
Running setup.py bdist_wheel for tabulate ... done
Stored in directory: /home/cmz/.cache/pip/wheels/7c/fc/c4/f89c90e8bb6a0052a4ad4a9bc30a61429fea5d3439c63e2efd
Successfully built tabulate
Installing collected packages: tabulate
Successfully installed tabulate-0.8.2 ## 流程圖 億圖 7.8 ## 功能介紹 1.人員信息增刪改查程序 * 涉及文件:user_info.py staff_table.db * 使用python3 運行user_info.py文件執行程序 * 程序說明: - 本系統共分為六大環節: 1. 顯示當前數據庫中的所有人員清單列表 2. 顯示輸入幫助信息 3. sql 語句輸入處理(四中操作) - 新增某條用戶信息(id自增,且用戶手機號是主鍵唯一) - 根據id號刪除某個用戶整條信息 - 根據條件修改用戶信息 - 根據條件查詢用戶信息

代碼:
#!/usr/bin/env python # _*_ coding: utf-8 _*_ """ @author: caimengzhi @license: (C) Copyright 2013-2017. @contact: 610658552@qq.com @software: pycharm 2017.02 @project: luffy @file: tmp.py @time: 2018/1/24 15:15 @desc: 1.可進行模糊查詢,語法至少支持下面3種查詢語法: find name,age from staff_table where age > 22 find * from staff_table where dept = "IT" find * from staff_table where enroll_date like "2013" 2.可創建新員工紀錄,以phone做唯一鍵(即不允許表里有手機號重復的情況),staff_id需自增 語法: add staff_table Alex Li,25,134435344,IT,2015-10-29 3.可刪除指定員工信息紀錄,輸入員工id,即可刪除 語法: del from staff where id=3 4.可修改員工信息,語法如下: UPDATE staff_table SET dept = "Market" WHERE dept = "IT" 把所有dept=IT的紀錄的dept改成Market UPDATE staff_table SET age = 25 WHERE name = "Alex Li" 把name=Alex Li的紀錄的年齡改成25 5.以上每條語名執行完畢后,要顯示這條語句影響了多少條紀錄。 比如查詢語句 就顯示 查詢出了多少條、修改語句就顯示修改了多少條等。 """ import os import re import time from tabulate import tabulate # 高亮顯示字體顏色 red = '\033[1;31;40m' # 紅色 green = '\033[1;32;40m' # 綠色 blue = '\033[1;34;40m' # 藍色 end = '\033[0m' # 無色 # 高亮閃爍顯示字體顏色 red_twinkle = '\033[5;31;40m' # 紅色,閃爍 green_twinkle = '\033[1;32;40m' # 綠色,閃爍 blue_twinkle = '\033[1;34;40m' # 藍色,閃爍 # 數據相關 data_path = "data" # 數據文件夾 staff_table = "staff_table" # 數據表名 staff_db = os.path.join(data_path, staff_table + '.db') # 路徑全稱 staff_db_swap = os.path.join(data_path, staff_table + '.swap') FIELD_LEN = 7 # 表中字段必須是5 # query keywords = { "star": "*", "from": "from", "where": "where" } field_symbol = { ">": ">", "<": "<", "=": "=", "like": "like" } field_keywords = { "staff_id": "staff_id", "name": "name", "age": "age", "phone": "phone", "dept": "dept", "enroll_date": "enroll_date" } sql_commands = { "find": "find", "add": "add", "del": "del", "update": "update" } # 字段比較> < = def show_basic_field(query_list_head_field,user_list_tmp): list_tmp, list_tmp1, list_tmp2 = [],[],[] for line in query_list_head_field: print(line, end=" ") list_tmp1.append(line) print("") for i in user_list_tmp: for j in query_list_head_field: print(i[get_feild_id(j)], end=" ") list_tmp2.append(i[get_feild_id(j)]) print("") def field_symbol_gt(user_list,query_list_head_field, position3, position1): user_list_tmp, count = [], 0 for k in user_list: if k[get_feild_id(position3)] > position1: count += 1 k_str = ','.join(k).replace("\n", "") k_str = k_str.replace(',', " ") # 將逗號替換成空格 user_list_tmp.append(k) show_basic_field(query_list_head_field, user_list_tmp) return count def field_symbol_lt(user_list,query_list_head_field, position3, position1): user_list_tmp, count = [], 0 for k in user_list: if k[get_feild_id(position3)] < position1: count += 1 k_str = ','.join(k).replace("\n", "") k_str = k_str.replace(',', " ") # 將逗號替換成空格 user_list_tmp.append(k) show_basic_field(query_list_head_field, user_list_tmp) return count def field_symbol_eq(user_list,query_list_head_field, position3, position1): user_list_tmp, count = [], 0 for k in user_list: if k[get_feild_id(position3)] < position1: count += 1 k_str = ','.join(k).replace("\n", "") k_str = k_str.replace(',', " ") # 將逗號替換成空格 user_list_tmp.append(k) show_basic_field(query_list_head_field, user_list_tmp) return count field_symbol_compare = { ">": field_symbol_gt, "<": field_symbol_lt, "=": field_symbol_eq } # 錯誤輸出 def msg_log(normal_data, color_data, font_color): """ :param normal_data: 輸出不帶顏色字體 :param color_data: 輸出帶顏色的字體 :param font_color: 輸出字體的顏色 :return: """ if not color_data.split(): print(font_color + normal_data + end) else: print(normal_data + font_color + color_data + end) # 初始化員工數據 def initial_employee_information(): """ 初始化數據庫,員工信息數據 :return: """ msg = """1,Alex Li,22,13651054608,IT,2013-04-01 2,Jack Wang,28,13451024608,HR,2015-01-07 3,Rain Wang,21,13451054608,IT,2017-04-01 4,Mack Qiao,44,15653354208,Sales,2016-02-01 5,Rachel Chen,23,13351024606,IT,2013-03-16 6,Eric Liu,19,18531054602,Marketing,2012-12-01 7,Chao Zhang,21,13235324334,Administration,2011-08-08 8,Kevin Chen,22,13151054603,Sales,2013-04-01 9,Shit Wen,20,13351024602,IT,2017-07-03 10,Shanshan Du,26,13698424612,Operation,2017-07-02""" if not os.path.exists(data_path): os.mkdir(data_path) if not os.path.exists(staff_db): with open(staff_db, "w", encoding="utf-8") as fd: fd.write(msg) msg_log("員工信息數據初始化完畢", "", blue) def show_prompt_information(): """ 提示信息,方便用戶根據提示語法輸入相關命令提示進行操作 :return: """ msg = """ ---------------------------------------- 迎進入員工信息增刪改查程序 ---------------------------------------- 語法操作: 1.可進行模糊查詢: find name,age from staff_table where age > 22 find * from staff_table where dept = "IT" find * from staff_table where enroll_date like "2013" 2.可創建新員工紀錄,以phone做唯一鍵(即不允許表里有手機號重復的情況),staff_id需自增 語法: add staff_table Alex Li,18,134435344,IT,2015-10-29 3.可刪除指定員工信息紀錄,輸入員工id,即可刪除 語法: del from staff_tables where id=3 4.可修改員工信息,語法如下: UPDATE staff_table SET dept = "Market" WHERE dept = "IT" 把所有dept=IT的紀錄的dept改成Market UPDATE staff_table SET age = 25 WHERE name = "Alex Li" 把name=Alex Li的紀錄的年齡改成25 命令提示: 1. 輸入q/Q 退出當前應用 2. 輸入 show 查看當前數據人員名單 """ print(msg) def get_staff_id(staff_db): """ :param staff_db: 員工人員數據表 :return: 員工人員自增id """ list_data = [] with open(staff_db, 'r') as fd: for line in fd: list_data.append(line) auto_increment_id = list_data[-1].split(",")[0] return auto_increment_id def get_staff_info(dbfile): data_list = [] with open(dbfile, "r") as fd: for i in fd: data_list.append(str(i).split(',')) return data_list def get_feild_id(column): if column == "staff_id": column = 0 elif column == "name": column = 1 elif column == "age": column = 2 elif column == "phone": column = 3 elif column == "dept": column = 4 elif column == "enroll_date": column = 5 return column def tabulate_show(data_list): title_table = [["staff_id", "name", "age", "phone", "dept", "enroll_date"]] user_list = title_table + data_list print(tabulate(user_list)) def show_employee_info(): """ 格式化輸出當前數據庫里面人員名單 :return: """ user_list = get_staff_info(staff_db) tabulate_show(user_list) def show_tables(): """ 提示當前數據中表 :return: """ for file in os.listdir(data_path): if file.endswith(".db"): db_name = file.split(".db")[0] msg = """+----------------------------+ | employee_information | +----------------------------+ | %s | +----------------------------+ 1 rows in set (0.00 sec) """ % db_name print(msg) def check_table(table): """ :param table: 要判斷的數據庫的表 :return: 返回bool值(表存在就返回True,否則返回False) """ for file in os.listdir(data_path): if file.endswith(".db"): db_name = file.split(".db")[0] if db_name == table: return True else: return False def check_filed(table, filed): pass def list_remove_null(content): """ :param content: sql 語句 :return: 去除空的剩余sql語句 """ content_list = re.split(' |=', content) # 分解sql命令 while '' in content_list: # 去除sql列表命令中的空元素 content_list.remove('') return content_list def list_replace(content, old, new): """ 嵌套列表數據修改數據操作 :param content: 傳入2層列表(嵌套列表) :param old: 舊字段 :param new: 新字段 :return: """ p = content.index(old) content.remove(old) content.insert(p, new) return content def check_user(dbfile, field): """ 通過檢查唯一鍵filed,來檢查數據庫,用戶存在就返回True,否則返回False :param dbfile: 要檢查的數據庫 :param field: 要檢查的字段 :return: bool,True|False """ with open(dbfile, "r", encoding="utf-8") as fd: for line in fd.readlines(): if field in line: return True else: return False def storage_data(data_value, rewrite=True): """ :param data_value: 把列表data_value中的數據寫入文件 :return: """ if rewrite == True: with open(staff_db, "w", encoding="utf-8") as fd: for item in data_value: fd.write(",".join(item)) else: with open(staff_db, "a+", encoding="utf-8") as fd: fd.write("\n") fd.write(",".join(data_value)) def query_operation(content): """ 根據條件查詢當前數據庫中數據 :param content: 傳入sql語句 :return: """ user_list_tmp = [] user_list = get_staff_info(staff_db) query_list = content.split() query_list_head, position1, position2, position3, position4 \ = query_list[0], query_list[-1], query_list[-2], query_list[-3], query_list[-4] # 去除 * ,name/age if position3 not in field_keywords: msg_log("抱歉,輸入命令錯誤,字段不正確,請打印h,查看規則", "", red) else: if query_list_head == '*': # 判斷查詢語句,關鍵字是否為*號 if position2 == field_symbol[">"]: pass elif position2 == field_symbol["<"]: pass elif position2 == field_symbol["="]: # 判斷類似語句 find * from staff_table where dept = "IT" count = 0 for k in user_list: if eval(position1) in k: count += 1 user_list_tmp.append(k) tabulate_show(user_list_tmp) elif position2 == field_symbol["like"]: # 判斷類似語句 find * from staff_table where enroll_date like "2013" count = 0 for k in user_list: if eval(position1) in k[get_feild_id(position3)]: count += 1 user_list_tmp.append(k) tabulate_show(user_list_tmp) else: msg_log("抱歉,輸入命令錯誤,字段不正確,請打印h,查看規則", "", red) else: # find name,age from staff_table where age >|<|= 22 三種情況 query_list_head_field = query_list_head.split(",") count = field_symbol_compare.get(position2)(user_list,query_list_head_field, position3, position1) msg_log("查詢到語句條數為: ", "%s" % count, green) # 打印出受影響的條數 def add_operation(content): """ :param content: 傳入新增用戶sql語句 :return: """ phone_list = [] table, content = content.strip().split(" ", 1) content = content.split(",") user_list = get_staff_info(staff_db) if len(content) != 5: # 判讀輸入sql的字段長度必須是5,否則打印命令錯誤 msg_log("抱歉,輸入命令錯誤,字段不正確,請打印h,查看規則", "", red) else: user_list = get_staff_info(staff_db) for line in range(len(user_list)): # 去除文件中空行 if ['\n'] in user_list: user_list.remove(['\n']) for i in range(len(user_list)): # 保存當前數據庫中的所有電話清單到列表 phone_list.append(user_list[i][3]) if content[2] in phone_list: # 判斷電話是否存在 msg_log("抱歉,用戶名已經存在", " ", red) else: increment_id = (len(user_list) + 1) content.insert(0, str(increment_id)) content.append("\n") storage_data(content, rewrite=False) msg_log("新增語句,影響的行數為", "%s" % 1, green) def del_operation(content): """ 根據id號刪除用戶數據 :param content: sql語句內容 :return: """ tmp_id, tmp_id_flag, staff_id = 0, True, [] # 初始化臨時參數 del_id = int(content.split()[-1].split("=")[-1]) - 1 # 獲取要刪除的id user_list = get_staff_info(staff_db) # 保存當前數據庫中的所有電話清單到列表 for i in range(len(user_list)): if str(del_id + 1) == user_list[i][0]: tmp_id, tmp_id_flag = i, True break else: tmp_id_flag = False if not tmp_id_flag: msg_log("抱歉,你要刪除的數據不存在", "", red) else: del user_list[tmp_id] storage_data(user_list) msg_log("刪除語句,影響的行數為", "%s" % 1, green) def update_operation(content): """ :param content: 傳入查詢sql語句 :return: """ update_keywords, content_list, data_value_tmp, count = ["set", "where"], [], [], 0 data_value = get_staff_info(staff_db) content_list = list_remove_null( content) # 分解sql命令 ['staff_table', 'SET', 'dept', '"Market"', 'WHERE', 'dept', '"IT"'] if content_list[0] == "staff_table": # 判讀數據表,關鍵字例如where,set和要查詢的字段例如name必須存存在(合法) # 判斷sql 語法 合法性 if content_list[1].lower() in update_keywords and content_list[4].lower() in update_keywords and \ content_list[2].lower() in field_keywords and content_list[5] in field_keywords: # 修改相同列, 例如 UPDATE staff_table SET dept="Market" WHERE dept = "IT" if content_list[2] == content_list[5]: for line in data_value: if eval(content_list[6]) in line[get_feild_id(content_list[5])]: count += 1 line = list_replace(line, eval(content_list[6]), eval(content_list[3])) data_value_tmp.append(line) storage_data(data_value_tmp) else: # 修改不同列,UPDATE staff_table SET age=25 WHERE name = "Alex Li" if 'WHERE' in content: pre_content, after_content = content.split("WHERE") pre_content = pre_content.split() old_field_key = after_content.split("=")[0].split()[0] old_field_value = eval(after_content.split("=")[-1]) new_filed_key, new_filed_value = pre_content[2], pre_content[4] for line in data_value: if old_field_value in line: count += 1 list_replace(line, line[get_feild_id(new_filed_key)], new_filed_value) data_value_tmp.append(line) storage_data(data_value_tmp) msg_log("修改語句條數為: ", "%s" % count, green) # 打印出受影響的條數 else: msg_log("抱歉,你輸入的SQL語法錯誤:", " ", red) else: msg_log("抱歉,你的輸入的數據庫表不存在:", " ", red) def parse_input_data(sql): """ 解釋輸入數據, 1. 解析動作,是add,find,del,update 2. 解釋內容,sql的具體操作 :param sql: 操作sql語句 :return: """ command, content = sql.split(' ', 1) msg_log("本次執行操作語句:", "%s" % sql, blue) if command.lower() == sql_commands["find"]: # 查詢操作 query_operation(content) elif command.lower() == sql_commands["add"]: # 新增操作 add_operation(content) elif command.lower() == sql_commands["del"]: # 刪除操作 del_operation(content) elif command.lower() == sql_commands["update"]: # 更新操作 update_operation(content) else: msg_log("抱歉,你輸入的SQL命令不存在:", "%s" % sql, red) def main_process(): """ 主函數通過輸入,匹配相對應的功能,解析sql,對應輸出數據 :return: """ initial_employee_information() query_status = True while query_status: input_data = input("請輸入操作語句,<h|H 幫助>: ").strip() if input_data.lower() == "q": # 輸入q/Q 退出操作 query_status = False elif not input_data: msg_log("抱歉,輸入不能為空", "", red) elif input_data.lower() == "h": # 展示提示信息,以便按照語法輸入 show_prompt_information() elif input_data.lower().split()[0] == "show": # 查看人員信息 show_employee_info() else: if input_data.split()[0].lower() in sql_commands and len(input_data.split()) > 3: parse_input_data(input_data) # 解析輸入的sql語法 else: msg_log("抱歉,sql命令不存在: ", "%s" % input_data, red) if __name__ == "__main__": main_process()
測試記錄:
1. 顯示人員清單
cmz@leco:~/test$ python3 user_info.py 員工信息數據初始化完畢 請輸入操作語句,<h|H 幫助>: h ---------------------------------------- 迎進入員工信息增刪改查程序 ---------------------------------------- 語法操作: 1.可進行模糊查詢: find name,age from staff_table where age > 22 find * from staff_table where dept = "IT" find * from staff_table where enroll_date like "2013" 2.可創建新員工紀錄,以phone做唯一鍵(即不允許表里有手機號重復的情況),staff_id需自增 語法: add staff_table Alex Li,18,134435344,IT,2015-10-29 3.可刪除指定員工信息紀錄,輸入員工id,即可刪除 語法: del from staff_tables where id=3 4.可修改員工信息,語法如下: UPDATE staff_table SET dept = "Market" WHERE dept = "IT" 把所有dept=IT的紀錄的dept改成Market UPDATE staff_table SET age = 25 WHERE name = "Alex Li" 把name=Alex Li的紀錄的年齡改成25 命令提示: 1. 輸入q/Q 退出當前應用 2. 輸入 show 查看當前數據人員名單 請輸入操作語句,<h|H 幫助>: show -------- ----------- --- ----------- -------------- ----------- staff_id name age phone dept enroll_date 1 Alex Li 22 13651054608 IT 2013-04-01 2 Jack Wang 28 13451024608 HR 2015-01-07 3 Rain Wang 21 13451054608 IT 2017-04-01 4 Mack Qiao 44 15653354208 Sales 2016-02-01 5 Rachel Chen 23 13351024606 IT 2013-03-16 6 Eric Liu 19 18531054602 Marketing 2012-12-01 7 Chao Zhang 21 13235324334 Administration 2011-08-08 8 Kevin Chen 22 13151054603 Sales 2013-04-01 9 Shit Wen 20 13351024602 IT 2017-07-03 10 Shanshan Du 26 13698424612 Operation 2017-07-02 -------- ----------- --- ----------- -------------- -----------
其中初始化會,產生數據,一旦數據產生后,下次登錄后就檢查文件夾和文件是否存在,存在就不創建了。
cmz@leco:~/test$ tree .
.
├── data
│ └── staff_table.db
└── user_info.py
1 directory, 2 files
2. 顯示幫助
請輸入操作語句,<h|H 幫助>: h ---------------------------------------- 迎進入員工信息增刪改查程序 ---------------------------------------- 語法操作: 1.可進行模糊查詢: find name,age from staff_table where age > 22 find * from staff_table where dept = "IT" find * from staff_table where enroll_date like "2013" 2.可創建新員工紀錄,以phone做唯一鍵(即不允許表里有手機號重復的情況),staff_id需自增 語法: add staff_table Alex Li,18,134435344,IT,2015-10-29 3.可刪除指定員工信息紀錄,輸入員工id,即可刪除 語法: del from staff_tables where id=3 4.可修改員工信息,語法如下: UPDATE staff_table SET dept = "Market" WHERE dept = "IT" 把所有dept=IT的紀錄的dept改成Market UPDATE staff_table SET age = 25 WHERE name = "Alex Li" 把name=Alex Li的紀錄的年齡改成25 命令提示: 1. 輸入q/Q 退出當前應用 2. 輸入 show 查看當前數據人員名單 請輸入操作語句,<h|H 幫助>:
3. 顯示查詢
請輸入操作語句,<h|H 幫助>: h ---------------------------------------- 迎進入員工信息增刪改查程序 ---------------------------------------- 語法操作: 1.可進行模糊查詢: find name,age from staff_table where age > 22 find * from staff_table where dept = "IT" find * from staff_table where enroll_date like "2013" 2.可創建新員工紀錄,以phone做唯一鍵(即不允許表里有手機號重復的情況),staff_id需自增 語法: add staff_table Alex Li,18,134435344,IT,2015-10-29 3.可刪除指定員工信息紀錄,輸入員工id,即可刪除 語法: del from staff_tables where id=3 4.可修改員工信息,語法如下: UPDATE staff_table SET dept = "Market" WHERE dept = "IT" 把所有dept=IT的紀錄的dept改成Market UPDATE staff_table SET age = 25 WHERE name = "Alex Li" 把name=Alex Li的紀錄的年齡改成25 命令提示: 1. 輸入q/Q 退出當前應用 2. 輸入 show 查看當前數據人員名單 請輸入操作語句,<h|H 幫助>: find name,age from staff_table where age > 22 本次執行操作語句:find name,age from staff_table where age > 22 name age Jack Wang 28 Mack Qiao 44 Rachel Chen 23 Shanshan Du 26 查詢到語句條數為: 4 請輸入操作語句,<h|H 幫助>: find * from staff_table where dept = "IT" 本次執行操作語句:find * from staff_table where dept = "IT" -------- ----------- --- ----------- ---- ----------- staff_id name age phone dept enroll_date 1 Alex Li 22 13651054608 IT 2013-04-01 3 Rain Wang 21 13451054608 IT 2017-04-01 5 Rachel Chen 23 13351024606 IT 2013-03-16 9 Shit Wen 20 13351024602 IT 2017-07-03 -------- ----------- --- ----------- ---- ----------- 請輸入操作語句,<h|H 幫助>: find * from staff_table where enroll_date like "2013" 本次執行操作語句:find * from staff_table where enroll_date like "2013" -------- ----------- --- ----------- ----- ----------- staff_id name age phone dept enroll_date 1 Alex Li 22 13651054608 IT 2013-04-01 5 Rachel Chen 23 13351024606 IT 2013-03-16 8 Kevin Chen 22 13151054603 Sales 2013-04-01 -------- ----------- --- ----------- ----- -----------
4. 顯示新增用戶
請輸入操作語句,<h|H 幫助>: show -------- ----------- --- ----------- -------------- ----------- staff_id name age phone dept enroll_date 1 Alex Li 22 13651054608 IT 2013-04-01 2 Jack Wang 28 13451024608 HR 2015-01-07 3 Rain Wang 21 13451054608 IT 2017-04-01 4 Mack Qiao 44 15653354208 Sales 2016-02-01 5 Rachel Chen 23 13351024606 IT 2013-03-16 6 Eric Liu 19 18531054602 Marketing 2012-12-01 7 Chao Zhang 21 13235324334 Administration 2011-08-08 8 Kevin Chen 22 13151054603 Sales 2013-04-01 9 Shit Wen 20 13351024602 IT 2017-07-03 10 Shanshan Du 26 13698424612 Operation 2017-07-02 -------- ----------- --- ----------- -------------- ----------- 請輸入操作語句,<h|H 幫助>: add staff_table Alex Li,18,134435344,IT,2015-10-29 本次執行操作語句:add staff_table Alex Li,18,134435344,IT,2015-10-29 新增語句,影響的行數為1 請輸入操作語句,<h|H 幫助>: show -------- ----------- --- ----------- -------------- ----------- staff_id name age phone dept enroll_date 1 Alex Li 22 13651054608 IT 2013-04-01 2 Jack Wang 28 13451024608 HR 2015-01-07 3 Rain Wang 21 13451054608 IT 2017-04-01 4 Mack Qiao 44 15653354208 Sales 2016-02-01 5 Rachel Chen 23 13351024606 IT 2013-03-16 6 Eric Liu 19 18531054602 Marketing 2012-12-01 7 Chao Zhang 21 13235324334 Administration 2011-08-08 8 Kevin Chen 22 13151054603 Sales 2013-04-01 9 Shit Wen 20 13351024602 IT 2017-07-03 10 Shanshan Du 26 13698424612 Operation 2017-07-02 11 Alex Li 18 134435344 IT 2015-10-29 -------- ----------- --- ----------- -------------- ----------- 請輸入操作語句,<h|H 幫助>: 抱歉,輸入不能為空 請輸入操作語句,<h|H 幫助>: add staff_table Alex Li,18,134435344,IT,2015-10-29 本次執行操作語句:add staff_table Alex Li,18,134435344,IT,2015-10-29 抱歉,用戶名已經存在
此時新增的用戶,追加在文件末尾,且根據phone作為唯一主鍵,staff_id自增。
5. 根據id刪除某條用戶信息
請輸入操作語句,<h|H 幫助>: show -------- ----------- --- ----------- -------------- ----------- staff_id name age phone dept enroll_date 1 Alex Li 22 13651054608 IT 2013-04-01 2 Jack Wang 28 13451024608 HR 2015-01-07 3 Rain Wang 21 13451054608 IT 2017-04-01 4 Mack Qiao 44 15653354208 Sales 2016-02-01 5 Rachel Chen 23 13351024606 IT 2013-03-16 6 Eric Liu 19 18531054602 Marketing 2012-12-01 7 Chao Zhang 21 13235324334 Administration 2011-08-08 8 Kevin Chen 22 13151054603 Sales 2013-04-01 9 Shit Wen 20 13351024602 IT 2017-07-03 10 Shanshan Du 26 13698424612 Operation 2017-07-02 11 Alex Li 18 134435344 IT 2015-10-29 -------- ----------- --- ----------- -------------- ----------- 請輸入操作語句,<h|H 幫助>: del from staff_tables where id=3 本次執行操作語句:del from staff_tables where id=3 刪除語句,影響的行數為1 請輸入操作語句,<h|H 幫助>: del from staff_tables where id=3 本次執行操作語句:del from staff_tables where id=3 抱歉,你要刪除的數據不存在 請輸入操作語句,<h|H 幫助>: show -------- ----------- --- ----------- -------------- ----------- staff_id name age phone dept enroll_date 1 Alex Li 22 13651054608 IT 2013-04-01 2 Jack Wang 28 13451024608 HR 2015-01-07 4 Mack Qiao 44 15653354208 Sales 2016-02-01 5 Rachel Chen 23 13351024606 IT 2013-03-16 6 Eric Liu 19 18531054602 Marketing 2012-12-01 7 Chao Zhang 21 13235324334 Administration 2011-08-08 8 Kevin Chen 22 13151054603 Sales 2013-04-01 9 Shit Wen 20 13351024602 IT 2017-07-03 10 Shanshan Du 26 13698424612 Operation 2017-07-02 11 Alex Li 18 134435344 IT 2015-10-29 -------- ----------- --- ----------- -------------- -----------
根據id刪除,刪除后,再次刪除就顯示已經不存在了
6. 修改記錄
請輸入操作語句,<h|H 幫助>: show -------- ----------- --- ----------- -------------- ----------- staff_id name age phone dept enroll_date 1 Alex Li 22 13651054608 IT 2013-04-01 2 Jack Wang 28 13451024608 HR 2015-01-07 4 Mack Qiao 44 15653354208 Sales 2016-02-01 5 Rachel Chen 23 13351024606 IT 2013-03-16 6 Eric Liu 19 18531054602 Marketing 2012-12-01 7 Chao Zhang 21 13235324334 Administration 2011-08-08 8 Kevin Chen 22 13151054603 Sales 2013-04-01 9 Shit Wen 20 13351024602 IT 2017-07-03 10 Shanshan Du 26 13698424612 Operation 2017-07-02 11 Alex Li 18 134435344 IT 2015-10-29 -------- ----------- --- ----------- -------------- ----------- 請輸入操作語句,<h|H 幫助>: UPDATE staff_table SET dept = "Market" WHERE dept = "IT" 本次執行操作語句:UPDATE staff_table SET dept = "Market" WHERE dept = "IT" 修改語句條數為: 4 請輸入操作語句,<h|H 幫助>: UPDATE staff_table SET dept = "Market" WHERE dept = "IT" 本次執行操作語句:UPDATE staff_table SET dept = "Market" WHERE dept = "IT" 修改語句條數為: 0 請輸入操作語句,<h|H 幫助>: show -------- ----------- --- ----------- -------------- ----------- staff_id name age phone dept enroll_date 1 Alex Li 22 13651054608 Market 2013-04-01 2 Jack Wang 28 13451024608 HR 2015-01-07 4 Mack Qiao 44 15653354208 Sales 2016-02-01 5 Rachel Chen 23 13351024606 Market 2013-03-16 6 Eric Liu 19 18531054602 Marketing 2012-12-01 7 Chao Zhang 21 13235324334 Administration 2011-08-08 8 Kevin Chen 22 13151054603 Sales 2013-04-01 9 Shit Wen 20 13351024602 Market 2017-07-03 10 Shanshan Du 26 13698424612 Operation 2017-07-02 11 Alex Li 18 134435344 Market 2015-10-29 -------- ----------- --- ----------- -------------- -----------
修改的同時顯示修改了多少條記錄。
請輸入操作語句,<h|H 幫助>: show -------- ----------- --- ----------- -------------- ----------- staff_id name age phone dept enroll_date 1 Alex Li 25 13651054608 Market 2013-04-01 2 Jack Wang 28 13451024608 HR 2015-01-07 4 Mack Qiao 44 15653354208 Sales 2016-02-01 5 Rachel Chen 23 13351024606 Market 2013-03-16 6 Eric Liu 19 18531054602 Marketing 2012-12-01 7 Chao Zhang 21 13235324334 Administration 2011-08-08 8 Kevin Chen 22 13151054603 Sales 2013-04-01 9 Shit Wen 20 13351024602 Market 2017-07-03 10 Shanshan Du 26 13698424612 Operation 2017-07-02 11 Alex Li 25 134435344 Market 2015-10-29 -------- ----------- --- ----------- -------------- ----------- 請輸入操作語句,<h|H 幫助>: UPDATE staff_table SET age = 18 WHERE name = "Alex Li" 本次執行操作語句:UPDATE staff_table SET age = 18 WHERE name = "Alex Li" 修改語句條數為: 2 請輸入操作語句,<h|H 幫助>: UPDATE staff_table SET age = 18 WHERE name = "Alex Li" 本次執行操作語句:UPDATE staff_table SET age = 18 WHERE name = "Alex Li" 修改語句條數為: 2 請輸入操作語句,<h|H 幫助>: show -------- ----------- --- ----------- -------------- ----------- staff_id name age phone dept enroll_date 1 Alex Li 18 13651054608 Market 2013-04-01 2 Jack Wang 28 13451024608 HR 2015-01-07 4 Mack Qiao 44 15653354208 Sales 2016-02-01 5 Rachel Chen 23 13351024606 Market 2013-03-16 6 Eric Liu 19 18531054602 Marketing 2012-12-01 7 Chao Zhang 21 13235324334 Administration 2011-08-08 8 Kevin Chen 22 13151054603 Sales 2013-04-01 9 Shit Wen 20 13351024602 Market 2017-07-03 10 Shanshan Du 26 13698424612 Operation 2017-07-02 11 Alex Li 18 134435344 Market 2015-10-29 -------- ----------- --- ----------- -------------- -----------
不同類修改,顯示修改了多少條記錄,滿足就修改。
