为了账号安全,请及时绑定邮箱和手机立即绑定

python+MongoDB实现通讯录功能

标签:
Python

需求:实现类似于通讯录的功能,可以添加、删除、修改、搜索人员信息

from pymongo import MongoClient
'''Mongo连接'''
client = MongoClient('127.0.0.1', 5000)
db = client.test
collection = db.student
class person:
def addcon(self):
name = input('请输入要添加的联系人姓名:')
find = db.student.find({'姓名':name},{'姓名':1,'_id':0})
if find.count()!=0:
print('您输入的联系人已经存在!')
else:
tele = input('请输入联系人的电话:')
addr = input('请输入联系人的地址:')
db.student.save({'姓名':name,'号码':tele,'地址':addr})
def delecon(self):
name = input('请输入您要删除的联系人的姓名:')
find = db.student.find({'姓名': name}, {'姓名': 1, '_id': 0})
if find.count()==0:
print('您输入的姓名不存在!')
else:
db.student.remove({'姓名':name})
print('%s已经被删除' % name)
def serch(self):
name = input('请输入你要搜索的联系人的名字:')
find = db.student.find({'姓名':name},{'姓名':1,'_id':0})
if find.count()==0:
print('没这人啊,你是不是傻!')
else:
for ren in db.student.find({'姓名':name}):
print(ren)
def modify(self):
name = input('你要修改谁的信息,说!')
find = db.student.find({'姓名': name}, {'姓名': 1, '_id': 0})
if find.count() == 0:
print('没这人啊,你是不是傻')
else:
newtele = input('新电话嘞:')
newaddr = input('新地址嘞:')
db.student.update({'姓名':name},{set:{'电话':newtele,'地址':newaddr}})
def show(self):
for f in db.student.find({},{'_id':0}):
print(f)
def menu(self):
print('''功能菜单列表如下
1.添加
2.删除
3.搜索
4.修改
5.查看所有
6.退出
''')
people = person()
while True:
people.menu()
choice = int(input('请输入响应操作码:'))
if choice == 1:
people.addcon()
elif choice == 2:
people.delecon()
elif choice == 3:
people.serch()
elif choice == 4:
people.modify()
elif choice == 5:
people.show()
elif choice == 6:
break
else:
print('输入数字啊,大哥!')

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消