# -*- coding:utf-8 -*-
# @Author: mh
# @Time: 2018/3/25 14:49
# @说明:
"""对名片管理系统进行改进
1. 进行必要的封装,从面向过程改为面对对象的处理方式
2. 用数据库的形式将信息存储在数据库中
1.当用户对数据进行修改时,实际是对数据库进行修改
2.查看等等一些列操作也是通过SQL语句完成
"""
import time
from ly_05_框架函数 import frame
from pymysql import connect
"""定义一个类,定义单个名片对象
1. 对对象的初始化
2. 存储
3. 修改
4. 查看帮助
以下操作都是基于对于mysql数据库的交互
mysql -uroot -p
create database xxx charset="utf8";
use xxx(数据库名);
create table name_card(
id tinyint unsigned auto_increment primary key not null,
name varchar(20),
age tinyint unsigned,
gender enum("男","女"),
height decimal(5,2),
tell_phone varchar(11),
e_mail varchar(30),
birthday varchar(20)
);"""
class NameCard(object):
def __init__(self):
# 建立链接,创建光标
self.ly_connect = connect(host="localhost",port=3306,user="root",
password="mysql",database="ly_python",
charset="utf8")
self.ly_cursor = self.ly_connect.cursor()
def show(self,count,tuple):
print("--id-----name----age--gender---height-----tell_phone------------e_mail------------birthday---")
for i in range(count):
list = []
for string in tuple[i]:
string = str(string)
list.append(string)
print(list[0].ljust(4) + " " + list[1].center(10) + list[2].center(5) +
list[3].center(8) + list[4].center(10) + list[5].center(16) +
list[6].center(24) + list[7].center(14))
def insert_data(self):
while True:
print("---名片创建---")
print(">>>")
print("")
id = 0 # int
name = input("姓名:") # varchar
age = int(input("年龄:")) # int
gender = int(input("性别[1,男生 2,女生]:")) # 枚举类型,男 或者 女 1 或者 2
height = float(input("身高[cm]:")) # decimal 小数
tell_phone = input("手机号码:") # varchar
e_mail = input("邮箱:") # varchar 这里要特别注意sql语句,有哪些特殊字符不能当做普通的字符输入
birthday = input("出生年月[xxxx_xx_xx]:") # varchar
result_tuple = (id, name, age, gender, height, tell_phone, e_mail, birthday)
self.ly_cursor.execute("insert into name_card VALUES(%d,'%s',%d,%d,%5.2f,'%s','%s','%s');" % result_tuple)
self.ly_connect.commit()
a = input("名片创建成功,是否继续[1]继续 [其余键]返回:")
if a != "1":
break
@staticmethod
def show_windows():
print("-" * 100)
print("请选择查看名片的方式:")
print("")
print("1>> 查看所有名片")
print("2>> 根据姓名搜索")
print(" [注]你只需输入关键字,会自动匹配符合的全部内容")
print("3>> 根据年龄搜索")
print(" [注]输入对应的年龄区间")
print("4>> 根据性别搜索")
print("")
print("1>> 按年龄排序")
print("2>> 按性别排序")
print("3>> 按姓名排序")
print("0>> 反序")
def show_data(self):
while True:
self.show_windows()
a = input("请输入你想要搜索的选项[0]退出:")
if a == "0":
break
else:
b = input("请输入排序方式[注]可以采用多重排序方式,例如[123]按年龄排序,如果年龄相同,按性别排序,如果性别相同,按姓名排序,"
"如果想要反序查看的话,在后面再加0:")
string = self.rank_show(b)
if a == "1":
print("---所有名片---")
count = self.ly_cursor.execute("select * from name_card order by %s;" % string)
tuple = self.ly_cursor.fetchall()
self.show(count,tuple)
m = input("请输入任意键退出:")
if m:
continue
if a == "2":
s = input("请输入姓名关键字:")
count = self.ly_cursor.execute("select * from name_card where name LIKE '%' + %s + '%' ORDER BY %s;" % (s,string))
tuple = self.ly_cursor.fetchall()
self.show(count, tuple)
m = input("请输入任意键退出:")
if m:
continue
if a == "3":
while True:
try:
top = int(input("请输入查询年龄的上限:"))
buttom = int(input("请输入查询年龄的下限:"))
except:
print("请输入正确的信息")
continue
else:
count = self.ly_cursor.execute(
"select * from name_card where age between %d and %d ORDER BY %s;" % (buttom, top))
tuple = self.ly_cursor.fetchall()
self.show(count, tuple)
break
m = input("请输入任意键退出:")
if m:
continue
if a == "4":
while True:
try:
gender = int(input("请输入你想要查询的性别[0]男生 [1]女生:"))
except:
print("请输入正确的信息")
continue
else:
count = self.ly_cursor.execute(
"select * from name_card where gender=%d ORDER BY %s;" % gender)
tuple = self.ly_cursor.fetchall()
self.show(count, tuple)
break
m = input("请输入任意键退出:")
if m:
continue
def rank_show(self,b):
# 根据用户输入的排序方式,返回语句,也就是一个字符串
list = [4] * 4
list_result = []
desc = 0
if not b or b[0] not in ["0","1","2","3"]:
list_result.append("id")
else:
for i in range(len(b)):
list[i] = b[i]
for a in list:
if a == "1":
list_result.append("age")
elif a == "2":
list_result.append("gender")
elif a == "3":
list_result.append("name")
elif a == "0":
desc = 1
if list_result == []:
list_result.append("id")
break
else:
pass
if desc == 1:
list_result[0] += " desc"
result = ",".join(list_result)
return result
def update_data(self):
while True:
r_name = input("请输入你想要修改的名片的姓名信息[退出]直接回车:")
if r_name:
pr_name = [r_name]
count = self.ly_cursor.execute("select * from name_card where name=%s" ,pr_name)
tuple = self.ly_cursor.fetchall()
if tuple:
self.show(count,tuple)
r_name = int(input("是否要对数据进行修改[ ]输入你想要修改的id [0]否:"))
if r_name == 0:
continue
else:
print("---修改数据,不需要修改的直接回车")
self.ly_cursor.execute("select * from name_card where id=%d" % r_name)
tuple = self.ly_cursor.fetchall()
self.show(1,tuple)
while True:
name = input("姓名:")
if name:
self.ly_cursor.execute("update name_card set name='%s' WHERE id=%d" % (name,r_name))
age = input("年龄:")
if age:
self.ly_cursor.execute("update name_card set age=%d WHERE id=%d" % (int(age),r_name))
gender = input("性别[1,男生 2,女生]:")
if gender:
self.ly_cursor.execute("update name_card set gender=%d WHERE id=%d" % (int(gender),r_name))
height = input("身高[cm]:")
if height:
self.ly_cursor.execute("update name_card set height=%5.2f WHERE id=%d" % (float(height),r_name))
tell_phone = input("手机号码:")
if tell_phone:
self.ly_cursor.execute("update name_card set tell_phone='%s' WHERE id=%d" % (tell_phone.center(15),r_name))
e_mail = input("邮箱:")
if e_mail:
self.ly_cursor.execute("update name_card set e_mail='%s' WHERE id=%d" % (e_mail.center(30),r_name))
birthday = input("出生年月[xxxx_xx_xx]:")
if birthday:
self.ly_cursor.execute("update name_card set birthday='%s' WHERE id=%d" % (birthday.center(15),r_name))
self.ly_connect.commit()
print("名片修改成功")
self.ly_cursor.execute("select * from name_card where id=%d" % r_name)
tuple = self.ly_cursor.fetchall()
self.show(1,tuple)
b = input("[任意键]继续修改 [直接回车]返回")
if not b:
break
else:
print("没有找到相关信息,请确认是否输入有误.")
continue
else:
break
# 帮助界面
@staticmethod
def help_windows():
print("*" * 100)
print("")
print("很高兴能够帮您解答疑惑!")
print("我们总共有四个选项可以供您选择,如果您想新建您的名片,您选择<1>\n \
然后按照提示输入您的信息即可。如果您想查看所有的名片信息,您选择<2>,而且我们提供了人性化的查看方式\n \
如果你想查找某个人的信息,您选择<3>,只需要输入他的名字,我们会自动输入他的信息,当然您还可以对信息进行修改")
print("")
print("如果您还有什么疑惑的话,可以拨打我们的热线电话进行咨询。")
print("tell:111111111")
print("*" * 100)
# 创建菜单函数
@staticmethod
def menu():
print("-" * 50)
print("欢迎使用lenmei名片系统!")
print("")
print("1>> 新建名片")
print("2>> 查看名片")
print("3>> 修改名片")
print("4>> 帮助")
print("")
print("0>>退出系统")
print("")
print("-" * 50)
# 开始创建主函数或者说是调用函数,要求是无限循环的函数,除非用户自己主动退出
def main_call(name_card):
# 新建文件,用于保存数据
while True:
name_card.menu()
i = input("请输入您想要执行的项目:")
if i in ["1", "2", "3", "4"]:
if i == "1":
b = input("您选择的操作是【新建名片】,是否继续[1]是[2]返回:")
if b != "1":
continue
# 自执新建名片操作,及添加数据
name_card.insert_data()
elif i == "2":
name_card.show_data()
elif i == "3":
b = input("您选择的操作是【搜索名片】,是否继续[1]是[2]返回:")
if b != "1":
continue
name_card.update_data()
else:
b = input("您选择的操作是【帮助】,是否继续[1]是[2]返回:")
if b != "1":
continue
name_card.help_windows()
s = input("输入任意字符回到主菜单!")
if s:
continue
elif i == "0":
b = input("您确定要退出系统吗?[1]是[2]返回主菜单:")
if b != "1":
continue
print("欢迎下次光临!")
break
else:
print("不好意思,您输入的信息有误,请再次输入。")
def main():
# 输出框架,欢迎界面
frame("welcome to the name_card management information system by ly")
time.sleep(2)
# 创建名片对象
name_card = NameCard()
# 主调用程序
main_call(name_card)
if __name__ == "__main__":
main()