python数据库编程数据库编程 Mysql实现通讯录实现通讯录
本文实例为大家分享了Mysql实现通讯录的具体代码,供大家参考,具体内容如下
#-*-code:utf-8-*-
import pymysql
def CreateTable():
hcon=pymysql.connect(host='localhost',user='root',password='lptpwd',database='contract',charset='utf8')
hcur=hcon.cursor()
hcur.execute('drop table if exists contractlist')
ctable='''
create table contractlist
(
ID int(10) primary key,
NAME varchar(20) not null,
TELF char(11) not null,
TELS char(11),
OTHER varchar(50)
)engine=myisam charset=utf8;
'''
hcur.execute(ctable)
hcur.close()
hcon.close()
def AddInfo(hcon,hcur):
id=int(input('please input ID: '))
name=str(input('please input Name: '))
telf=str(input('please input Tel 1: '))
tels=str(input('please input Tel 2: '))
other=str(input('please input other: '))
sql="insert into contractlist(id,name,telf,tels,other) values(%s,%s,%s,%s,%s)"
try:
hcur.execute(sql,(id,name,telf,tels,other))
hcon.commit()
except:
hcon.rollback()
def DeleteInfo(hcon,hcur):
SelectInfo(hcon,hcur)
did=int(input('please input id of delete: '))
sql="delete from contractlist where id=%s"
try:
hcur.execute(sql,(did,))
hcon.commit()
except:
hcon.rollback()
def UpdateInfo(hcon,hcur):
SelectInfo(hcon,hcur)
did=int(input('please input id of update: '))
sqlname="update contractlist set name=%s where id=%s"
name=str(input('please input Name: '))
try:
hcur.execute(sqlname,(name,did))
hcon.commit()
except:
hcon.rollback()
sqltelf="update contractlist set telf=%s where id=%s"
telf=str(input('please input Tel 1: '))
try:
hcur.execute(sqltelf,(telf,did))
hcon.commit()
except:
hcon.rollback()
sqltels="update contractlist set tels=%s where id=%s"
tels=str(input('please input Tel 2: '))
try:
hcur.execute(sqltels,(tels,did))
评论0
最新资源