python数据库编程 Mysql实现通讯录
作者:Ugex 发布时间:2024-01-24 17:53:11
标签: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))
hcon.commit()
except:
hcon.rollback()
sqlothers="update contractlist set other=%s where id=%s"
other=str(input('please input other: '))
try:
hcur.execute(sqlothers,(other,did))
hcon.commit()
except:
hcon.rollback()
def SelectInfo(hcon,hcur):
hcur.execute("select * from contractlist")
result=hcur.fetchall()
ptitle=('ID','Name','Tel 1','Tel 2','Other')
print(ptitle)
for findex in result:
print(findex)
print('')
def Meau():
print('1.diaplay')
print('2.add')
print('3.update')
print('4.delete')
print('5.cls')
print('0.exit')
sel=9
while(sel>5 or sel<0):
sel=int(input('please choice: '))
return sel
def main():
#CreateTable()
hcon=pymysql.connect(host='localhost',user='root',password='ltb12315',database='contract',charset='utf8')
hcur=hcon.cursor()
while(True):
sel=Meau()
if(sel==1):
SelectInfo(hcon,hcur)
elif(sel==2):
AddInfo(hcon,hcur)
elif(sel==3):
UpdateInfo(hcon,hcur)
elif(sel==4):
DeleteInfo(hcon,hcur)
elif(sel==5):
os.system('cls')
else:
break
print('-------------------------')
hcur.close()
hcon.close()
if __name__=="__main__":
main()
来源:https://blog.csdn.net/Ugex_Savelar/article/details/103056800
0
投稿
猜你喜欢
- asp十进制转二进制;二进制转十进制;二进制转十六进制;十六进制转二进制;八进制转二进制'二进制转八进制;八进制转十进制;十六进制转
- 本文实例讲述了GO语言Defer用法。分享给大家供大家参考。具体分析如下:defer:调用一个被 defer 的函数时在函数刚要返回之前延迟
- 前言这篇文章主要介绍了Python 字符串去除空格的6种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,来
- 一、如果models.py文件为时:timestamp = models.DateTimeField('保存日期')会提示:
- 元编程,一个听起来特别酷的词,强大的Lisp在这方面是好手,对于Python,尽管没有完善的元编程范式,一些天才的开发者还是创作了很多元编程
- 目录一、概念描述二、序列的可迭代性三、经典的迭代器模式四、生成器也是迭代器五、实现惰性迭代器六、使用生成器表达式简化惰性迭代器总结一、概念描
- 利用python 写一些网络服务的时候,当网络状况不好,或者资源占用过多,任务拥塞的情况下,总会抛出一些异常,当前任务就被终止了,可以很好的
- 抱着“取之于众 服务于众”的思想,我总结了一下,把它拿到网上来与大家分享,希望能帮助遇到类似问题的朋友。 我主要使用了IE内置的WebBro
- 本文实例为大家分享了JavaScript实现简易购物车的具体代码,供大家参考,具体内容如下代码:<!DOCTYPE html>&
- 前言相比java,python的异常和java中不同,python主要是防止程序异常被中止。一旦被catch后它还行往下执行。一、异常1.1
- 本文实例讲述了Pytorch实现的手写数字mnist识别功能。分享给大家供大家参考,具体如下:import torchimport torc
- 目录1 作用2 安装3 所有要运行的命令都放在 manager.py 中4 三种方式 创建命令4.1 方式一 创建 Command子类4.2
- 作为k8s官方维护的客户端,k8s go-client对于go语言中使用k8s可以说是唯一选项。但是官方的使用示例我个人觉得并不是很清晰,尤
- 文章主要讲术了一些SQL Server新的Bug,帮您认识这些被忽略的SQL Server注入技巧。1.关于Openrowset和Opend
- 前言:在涉及商品类的项目时,为了给同一类商品定位,往往会分等级或者成色。而等级/成色有时是用类似A,A+,A+1,K,L1,L2等英文与数字
- PHP使用星号替代用户名手机和邮箱这个在许多的活动界面会看到如淘宝的购物界面中的一些客户的支付宝号都是隐藏掉的哦,下面我们来看一下它的使用方
- 如果用到数据筛选功能,可以使用x if condition else y的逻辑实现。如果使用的是纯Python,可以使用不断迭代的方式对每一
- 安装python之后,我们往往面临这样一个问题,在命令行输入“python”,竟然出错,难道是没有安装成功吗?非也,其实是你的系统环境变量没
- asp之日期和时间函数示例可以使用日期和时间函数来得到各种格式的日期和时间函数语法说明示例NowNow()取得系统当前的日期和
- ▲ SHOW执行下面这个命令可以了解服务器的运行状态mysql >show status;该命令将显示出一长列状态