python数据库编程 Mysql实现通讯录

 更新时间:2020年03月27日 09:22:47   作者:Ugex  
这篇文章主要为大家详细介绍了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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Python基础知识之变量的详解

    Python基础知识之变量的详解

    这篇文章主要介绍了Python基础知识之变量的详解,文中有非常详细的代码示例,对正在学习python的小伙伴们有很好的帮助,需要的朋友可以参考下
    2021-04-04
  • Python数据类型之String字符串实例详解

    Python数据类型之String字符串实例详解

    这篇文章主要介绍了Python数据类型之String字符串,结合实例形式详细讲解了Python字符串的概念、定义、连接、格式化、转换、查找、截取、判断等常见操作技巧,需要的朋友可以参考下
    2019-05-05
  • python rolling regression. 使用 Python 实现滚动回归操作

    python rolling regression. 使用 Python 实现滚动回归操作

    这篇文章主要介绍了python rolling regression. 使用 Python 实现滚动回归操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • miniconda3介绍、安装以及使用教程

    miniconda3介绍、安装以及使用教程

    Miniconda是一款小巧的python环境管理工具,安装包大约只有50M多点,其安装程序中包含conda软件包管理器和Python,下面这篇文章主要给大家介绍了关于miniconda3介绍、安装以及使用的相关资料,需要的朋友可以参考下
    2023-02-02
  • Python实现统计给定列表中指定数字出现次数的方法

    Python实现统计给定列表中指定数字出现次数的方法

    这篇文章主要介绍了Python实现统计给定列表中指定数字出现次数的方法,涉及Python针对列表的简单遍历、计算相关操作技巧,需要的朋友可以参考下
    2018-04-04
  • 在python中画正态分布图像的实例

    在python中画正态分布图像的实例

    今天小编就为大家分享一篇在python中画正态分布图像的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • 详解如何利用Python制作24点小游戏

    详解如何利用Python制作24点小游戏

    这篇文章主要为大家详细介绍了如何通过Python制作24点小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Python实现扣除个人税后的工资计算器示例

    Python实现扣除个人税后的工资计算器示例

    这篇文章主要介绍了Python实现扣除个人税后的工资计算器,涉及Python流程控制与数学运算相关操作技巧,需要的朋友可以参考下
    2018-03-03
  • Python实现xml格式转txt格式的示例代码

    Python实现xml格式转txt格式的示例代码

    VOC 的标注是xml格式的,而YOLO是.txt格式,所以要实现VOC数据集转YOLO数据集,只能利用代码实现。所以本文为大家介绍了Python中xml转txt的示例代码,需要的可以参考一下
    2022-03-03
  • Python如何获取系统iops示例代码

    Python如何获取系统iops示例代码

    这篇文章主要是介绍用python通过系统数据来获取磁盘的iops,便于监控使用情况,对于大家在安全监控方面很实用,有需要的朋友们可以参考借鉴。
    2016-09-09

最新评论