python实现⼿机通讯录_Python结合MYSQL实现通讯录
时间:2018-12-12
概述:通讯录
python通讯录程序,在实现⽅⾯,数据库使⽤了mysql,连接形式采⽤sqlite3,其实程序⽐较简单,实现了基本的通讯录联系⼈添加、删除、修改、查看等功能,向初学者演⽰Python与数据库的⼀些简单操作技巧,代码中给出了丰富的注释,很有利于学习参考。
import sqlite3
#打开本地数据库⽤于存储⽤户信息
conn = t('mysql_person.db')
print "Opened database successfully";
'''
#在该数据库下创建表,创建表的这段代码在第⼀次执⾏后需要注释掉,否则再次执⾏程序会⼀直提⽰:
该表已存在
NULL,ADDRESS CHAR(50),SALARY REAL INT NOT NULL)")
print "Table created successfully";
conn.close()#关闭连接
php实训心得300字'''
#增加⽤户信息
def insert():
ID = input('请输⼊⽤户ID:\n'.decode('utf-8').encode('gb2312'))
NAME = raw_input('请输⼊⽤户昵称:\n'.decode('utf-8').encode('gb2312'))
AGE = input('请输⼊年龄:\n'.decode('utf-8').encode('gb2312'))
ADDRESS = raw_input('请输⼊⽤户地址:\n'.decode('utf-8').encode('gb2312'))
SALARY = input('请输⼊⽤户薪⽔:\n'.decode('utf-8').encode('gb2312'))
sql1 = 'insert into MT(ID,NAME,AGE,ADDRESS,SALARY)'
sql1 += 'values("%d","%s","%d","%s","%d");'%(ID,NAME,AGE,ADDRESS,SALARY)
connmit()#提交,否则⽆法保存
print "Records insert successfully"
#删除⽤户信息
def delete():
name = raw_input("请输⼊所要删除的联系⼈姓名:".decode('utf-8').encode('gb2312'))
cursor = ute("SELECT name from MT where name = '%s';"%name)
for row in cursor:
if name == row[0]:
connmit()
print "Records delete successfully"
break
else:
print "sorry,不存在该⽤户".decode('utf-8').encode('gb2312')
#修改⽤户信息
def modify():
name = raw_input("请输⼊要修改⽤户的姓名:".decode('utf-8').encode('gb2312'))
sql4 = "SELECT id, name, age,address, salary from MT where name = '%s';"%name cursor = ute(sql4)
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"
def change():
print '1.修改该⽤户地址'.decode('utf-8').encode('gb2312')
print '2.修改该⽤户年龄'.decode('utf-8').encode('gb2312')
print '3.修改该⽤户薪⽔'.decode('utf-8').encode('gb2312')
print '4.修改完毕,退出'.decode('utf-8').encode('gb2312')
while True:
change()
a=raw_input('请输⼊您的选择菜单号:'.decode('utf-8').encode('gb2312'))
if a=='1':
A = raw_input("请输⼊要修改⽤户的新地址:".decode('utf-8').encode('gb2312'))
sql31 = "UPDATE MT set address = '%s' where name = '%s';"%(A,name)
continue
if a=='2':
B = input("请输⼊要修改⽤户的新年龄:".decode('utf-8').encode('gb2312'))
sql32 = "UPDATE MT set age = '%d' where name = '%s';"%(B,name)
continue
if a=='3':
C = input("请输⼊要修改⽤户的新薪⽔:".decode('utf-8').encode('gb2312'))
sql33 = "UPDATE MT set salary = '%d' where name = '%s';"%(C,name)
continue
if a=='4':
print "感谢使⽤!".decode('utf-8').encode('gb2312')
break
connmit()
print "修改成功".decode('utf-8').encode('gb2312')
sql5 = "SELECT id, name, age,address, salary from MT where name = '%s';"%name cursor = ute(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 = t('mysql_person.db')
buttock什么意思
def search():
conn = t('mysql_person.db')
name = raw_input('请输⼊要查询的⽤户姓名:'.decode('utf-8').encode('gb2312'))
sql2 = "SELECT id,name,age, address, salary from MT where name= '%s';" % (name) cursor = ute(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,没有该⽤户信息".decode('utf-8').encode('gb2312')
#显⽰所有⽤户信息
def showall():
cursor = ute("SELECT id, name, age, 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 = ute("select count(*) from MT;")
for row in cursor:
print "⼀共有%d个⽤户".decode('utf-8').encode('gb2312')%row[0]
def menu():
print '1.新增联系⼈'.decode('utf-8').encode('gb2312')javascript实训心得体会
print '2.删除联系⼈'.decode('utf-8').encode('gb2312')
print '3.修改联系⼈'.decode('utf-8').encode('gb2312')
无限转笔教程print '4.查询联系⼈'.decode('utf-8').encode('gb2312')
print '5.显⽰所有联系⼈'.decode('utf-8').encode('gb2312')
print '6.退出程序'.decode('utf-8').encode('gb2312')
print 'What do you want to do?'
while True:
menu()
x = raw_input('请输⼊您的选择菜单号:'.decode('utf-8').encode('gb2312'))
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 "谢谢使⽤!".decode('utf-8').encode('gb2312')
exit()
continue
手机mysql安装配置教程
else:
print "输⼊的选项不存在,请重新输⼊!".decode('utf-8').encode('gb2312') continue

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。