python数据库编程 ODBC方式实现通讯录


Posted in Python onMarch 27, 2020

Python 数据库编程,ODBC方式实现通讯录,供大家参考,具体内容如下

#-*-coding:utf-8-*-
import pyodbc
import os
def SelectInfo(hcon,hcur):
 hcur.execute('select * from PassMapT')
 ptitle=('ID','Item','Pwd','other')
 print(ptitle)
 result=hcur.fetchall()
 for item in result:
 print(item)
 print('')

def AddInfo(hcon,hcur):
 id=int(input('please input ID: '))
 item=str(input('please input Item: '))
 pwd=str(input('please input Tel 1: '))
 other=str(input('please input Other: '))
 sql="insert into PassMapT(id,item,pwd,other) values(?,?,?,?)"
 try:
 hcur.execute(sql,(id,item,pwd,other))
 hcon.commit()
 except:
 hcon.rollback()

def DeleteInfo(hcon,hcur):
 SelectInfo(hcon,hcur)
 did=int(input('please input id of delete: '))
 sql="delete from PassMapT where id=?"
 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: '))
 
 sqlitem="update PassMapT set item=? where id=?"
 item=str(input('please input Item: '))
 try:
 hcur.execute(sqlitem,(item,did))
 hcon.commit()
 except:
 hcon.rollback()
 
 sqlpwd="update PassMapT set pwd=? where id=?"
 pwd=str(input('please input Pwd: '))
 try:
 hcur.execute(sqlpwd,(pwd,did))
 hcon.commit()
 except:
 hcon.rollback()
 
 sqlother="update PassMapT set other=? where id=?"
 other=str(input('please input other: '))
 try:
 hcur.execute(sqlother,(other,did))
 hcon.commit()
 except:
 hcon.rollback()
 
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():
 hcon = pyodbc.connect(r'DRIVER={SQL Server Native Client 11.0};SERVER=127.0.0.1;DATABASE=PasswordMap;UID=sa;PWD=lptpwd')
 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
 hcur.close()
 hcon.close()

if __name__=='__main__':
 main()

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

Python 相关文章推荐
Python常用模块介绍
Nov 21 Python
Python中数字以及算数运算符的相关使用
Oct 12 Python
Python正则表达式如何进行字符串替换实例
Dec 28 Python
Python实现简单过滤文本段的方法
May 24 Python
python实现判断一个字符串是否是合法IP地址的示例
Jun 04 Python
使用Anaconda3建立虚拟独立的python2.7环境方法
Jun 11 Python
Python中修改字符串的四种方法
Nov 02 Python
django-rest-swagger对API接口注释的方法
Aug 29 Python
Pycharm小白级简单使用教程
Jan 08 Python
Django values()和value_list()的使用
Mar 31 Python
python中if及if-else如何使用
Jun 02 Python
jupyter notebook远程访问不了的问题解决方法
Jan 11 Python
django 读取图片到页面实例
Mar 27 #Python
django ListView的使用 ListView中获取url中的参数值方式
Mar 27 #Python
django列表筛选功能的实现代码
Mar 27 #Python
python实现猜数游戏
Mar 27 #Python
手把手教你安装Windows版本的Tensorflow
Mar 26 #Python
python pandas.DataFrame.loc函数使用详解
Mar 26 #Python
Python计算指定日期是今年的第几天(三种方法)
Mar 26 #Python
You might like
在php中取得image按钮传递的name值
2006/10/09 PHP
实现dedecms全站URL静态化改造的代码
2007/03/29 PHP
PHP获取指定函数定义在哪个文件中以及其所在的行号实例
2014/05/08 PHP
php使用session二维数组实例
2014/11/06 PHP
jQuery 判断元素上是否绑定了事件
2009/10/28 Javascript
通过Jquery遍历Json的两种数据结构的实现代码
2011/01/19 Javascript
客户端js判断文件类型和文件大小即限制上传大小
2013/11/20 Javascript
jquery中html、val与text三者属性取值的联系与区别介绍
2013/12/29 Javascript
javascript基础知识
2016/06/07 Javascript
nodejs 实现钉钉ISV接入的加密解密方法
2017/01/16 NodeJs
js如何获取网页所有图片
2017/05/12 Javascript
提升页面加载速度的插件InstantClick
2017/09/12 Javascript
详解ECMAScript typeof用法
2018/07/25 Javascript
微信小程序使用map组件实现路线规划功能示例
2019/01/22 Javascript
Vue-CLI 3.X 部署项目至生产服务器的方法
2019/03/22 Javascript
[50:04]DOTA2上海特级锦标赛D组小组赛#2 Liquid VS VP第二局
2016/02/28 DOTA
使用Python装饰器在Django框架下去除冗余代码的教程
2015/04/16 Python
用Python实现服务器中只重载被修改的进程的方法
2015/04/30 Python
使用Python的web.py框架实现类似Django的ORM查询的教程
2015/05/02 Python
Python reduce()函数的用法小结
2017/11/15 Python
python 从csv读数据到mysql的实例
2018/06/21 Python
python批量处理txt文件的实例代码
2020/01/13 Python
使用Python实现将多表分批次从数据库导出到Excel
2020/05/15 Python
Python sorted对list和dict排序
2020/06/09 Python
如何在mac版pycharm选择python版本
2020/07/21 Python
python爬虫快速响应服务器的做法
2020/11/24 Python
一文彻底解决HTML5页面中长按保存图片功能
2019/06/10 HTML / CSS
Intimissimi德国网上商店:意大利知名内衣品牌
2018/04/03 全球购物
Java编程面试题
2016/04/04 面试题
六年级数学教学反思
2014/02/03 职场文书
数控技术专业毕业自荐书范文
2014/02/05 职场文书
个人自我剖析材料
2014/02/07 职场文书
爱的教育读书笔记
2015/06/26 职场文书
结婚喜宴迎宾词
2015/08/10 职场文书
详解运行Python的神器Jupyter Notebook
2021/06/03 Python
MySQL中优化SQL语句的方法(show status、explain分析服务器状态信息)
2022/04/09 MySQL