Python实现通讯录功能

yipeiwu_com5年前Python基础

说实话,第一次写这么长的Python代码,期间遇到了很多问题,但是,最终还是完成了,花了我一天半的时间。

该程序实现了用户的增,删,改,查,主要用到sqlite3模块。对于该模块的知识点,请查看我的另一篇文章SQLite-Python

如有更好的建议,请私信,本人将不胜荣幸,让我们一起来学习Python!

#-*- coding:utf-8 -*-
import sqlite3
#打开本地数据库用于存储用户信息
conn = sqlite3.connect('mysql_person.db')

#在该数据库下创建表,创建表的这段代码在第一次执行后需要注释掉,否则再次执行程序会一直提示:该表已存在
conn.execute('''CREATE TABLE MT
 (ID INT PRIMARY KEY NOT NULL,
 NAME TEXT NOT NULL,
 AGE INT NOT NULL,
 ADDRESS CHAR(50),
 SALARY REAL);''')
print "Table created successfully";
conn.close()

#增加用户信息
def insert():
 ID = input('请输入用户ID:\n')
 NAME = raw_input('请输入用户昵称:\n')
 AGE = input('请输入年龄:\n')
 ADDRESS = raw_input('请输入用户地址:\n')
 SALARY = input('请输入用户薪水:\n')
 sql1 = 'insert into MT(ID,NAME,AGE,ADDRESS,SALARY)'
 sql1 += 'values("%d","%s","%d","%s","%d");'%(ID,NAME,AGE,ADDRESS,SALARY)
 conn.execute(sql1)
 conn.commit()
 print "Records insert successfully"

#删除用户信息
def delete():
 name = raw_input("请输入所要删除的联系人姓名:")
 cursor = conn.execute("SELECT name from MT where name = '%s';"%name)
 for row in cursor:
 if name == row[0]:
 conn.execute("DELETE from MT where name = '%s';"%name)
 conn.commit()
 print "Records delete successfully"
 break
 else:
 print "sorry,不存在该用户"

#修改用户信息
def modify():
 name = raw_input("请输入要修改用户的姓名:")
 print search()
 sql4 = "SELECT id, name, age,address, salary from MT where name = '%s';"%name
 cursor = conn.execute(sql4)
 x = raw_input("请输入要修改用户的新地址:")
 y = input("请输入要修改用户的新年龄:")
 z = input("请输入要修改用户的新薪水:")
 sql3 = "UPDATE MT set address = '%s',age = '%d',\
 salary = '%d' where name = '%s';"%(x,y,z,name)
 conn.execute(sql3)
 conn.commit()
 print "修改成功"
 sql5 = "SELECT id, name, age,address, salary from MT where name = '%s';"%name
 cursor = conn.execute(sql5)
 for row in cursor:
 print "ID = ", row[0]
 print "NAME = ", row[1]
 print "AGE = ",row[2]
 print "ADDRESS = ", row[3]
 print "SALARY = ", row[4], "\n"


#查询用户信息
conn = sqlite3.connect('mysql_person.db')

def search():
 conn = sqlite3.connect('mysql_person.db')
 name = raw_input('请输入要查询的用户姓名')
 sql2 = "SELECT id,name,age, address, salary from MT where name= '%s';" % (name)
 cursor = conn.execute(sql2)
 for row in cursor:
 print "ID = ", row[0]
 print "NAME = ", row[1]
 print "AGE = ",row[2]
 print "ADDRESS = ", row[3]
 print "SALARY = ", row[4], "\n"
 break
 else:
 print "sorry,没有该用户信息"

#显示所有用户信息
def showall():
 cursor = conn.execute("SELECT id, age, name, address, salary from MT")
 for row in cursor:
 print "ID = ", row[0]
 print "NAME = ", row[1]
 print "AGE = ",row[2]
 print "ADDRESS = ", row[3]
 print "SALARY = ", row[4], "\n"
 print "Operation done successfully";
 cursor = conn.execute("select count(*) from MT;")
 for row in cursor:
 print "一共有%d个用户"%row[0]

def menu():
 print '1.新增联系人'
 print '2.删除联系人'
 print '3.修改联系人'
 print '4.查询联系人'
 print '5.显示所有联系人'
 print '6.退出程序'
 print 'What do you want to do?'
while True:
 menu()
 x = raw_input('请输入您的选择菜单号:')
 if x == '1':
 insert()
 continue
 if x == '2':
 delete()
 continue
 if x == '3':
 modify()
 continue
 if x == '4':
 search()
 continue
 if x == '5':
 showall()
 continue
 if x == '6':
 print "谢谢使用!"
 exit()
 continue
 else:
 print "输入的选项不存在,请重新输入!"
 continue

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python pip 安装与使用(安装、更新、删除)

pip 是 Python 包管理工具,该工具提供了对Python 包的查找、下载、安装、卸载的功能。 pip检测更新 命令:pip list –outdated pip升级包 命令...

Pandas DataFrame 取一行数据会得到Series的方法

Pandas DataFrame 取一行数据会得到Series的方法

Pandas DataFrame 取一行数据会得到Series的方法 如题,想要取如下dataframe的一行数据,以为得到的还是dataframe lista = [1, 3, 7...

12个步骤教你理解Python装饰器

前言 或许你已经用过装饰器,它的使用方式非常简单但理解起来困难(其实真正理解的也很简单),想要理解装饰器,你需要懂点函数式编程的概念,python函数的定义以及函数调用的语法规则等,虽然...

python多进程实现进程间通信实例

python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程。Python提供了非常好用的多进程包multiprocessi...

Python安装官方whl包和tar.gz包的方法(推荐)

Windows环境:   安装whl包:pip install wheel    ->    pip install&n...