python使用MySQLdb访问mysql数据库的方法


Posted in Python onAugust 03, 2015

本文实例讲述了python使用MySQLdb访问mysql数据库的方法。分享给大家供大家参考。具体如下:

#!/usr/bin/python
import MySQLdb
def doInsert(cursor,db):
 #insert
 # Prepare SQL query to INSERT a record into the database.
 sql = "UPDATE EMPLOYEE SET AGE = AGE+1 WHERE SEX = '%c'" %('M')
 try:
  cursor.execute(sql)
  db.commit()
 except:
  db.rollback()
def do_query(cursor,db):
 sql = "SELECT * FROM EMPLOYEE \
     WHERE INCOME > '%d'" % (1000)
 try:
   # Execute the SQL command
   cursor.execute(sql)
   # Fetch all the rows in a list of lists.
   results = cursor.fetchall()
   print 'resuts',cursor.rowcount
   for row in results:
    fname = row[0]
    lname = row[1]
    age = row[2]
    sex = row[3]
    income = row[4]
    # Now print fetched result
    print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
        (fname, lname, age, sex, income )
 except:
   print "Error: unable to fecth data"
def do_delete(cursor,db):
 sql = 'DELETE FROM EMPLOYEE WHERE AGE > {}'.format(20)
 try:
  cursor.execute(sql)
  db.commit()
 except:
  db.rollback()
def do_insert(cursor,db,firstname,lastname,age,sex,income):
 sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \
    LAST_NAME, AGE, SEX, INCOME) \
    VALUES ('%s', '%s', '%d', '%c', '%d' )" % \
    (firstname,lastname,age,sex,income)
 try:
  cursor.execute(sql)
  db.commit()
 except:
  db.rollback()
# Open database connection
# change this to your mysql account
#connect(server,username,password,db_name)
db = MySQLdb.connect("localhost","hunter","hunter","pydb" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
do_query(cursor,db)
doInsert(cursor,db)
do_query(cursor,db)
do_delete(cursor,db)
do_query(cursor,db)
do_insert(cursor,db,'hunter','xue',22,'M',2000)
do_insert(cursor,db,'mary','yang',22,'f',5555)
do_insert(cursor,db,'zhang','xue',32,'M',5000)
do_insert(cursor,db,'hunter','xue',22,'M',333)
do_query(cursor,db)
# disconnect from server
db.close()

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python备份Mysql脚本
Aug 11 Python
python字典序问题实例
Sep 26 Python
深入浅析python中的多进程、多线程、协程
Jun 22 Python
Python + selenium + requests实现12306全自动抢票及验证码破解加自动点击功能
Nov 23 Python
Django开发的简易留言板案例详解
Dec 04 Python
python实现简单图书管理系统
Nov 22 Python
Python.append()与Python.expand()用法详解
Dec 18 Python
Python使用多进程运行含有任意个参数的函数
May 02 Python
python3.8.1+selenium实现登录滑块验证功能
May 22 Python
python神经网络编程实现手写数字识别
May 27 Python
Python3+SQLAlchemy+Sqlite3实现ORM教程
Feb 16 Python
python中对列表的删除和添加方法详解
Feb 24 Python
浅谈Python中列表生成式和生成器的区别
Aug 03 #Python
详解Python3中的Sequence type的使用
Aug 01 #Python
将Python代码嵌入C++程序进行编写的实例
Jul 31 #Python
Python制作数据导入导出工具
Jul 31 #Python
简单理解Python中的装饰器
Jul 31 #Python
python简单分割文件的方法
Jul 30 #Python
Python读取网页内容的方法
Jul 30 #Python
You might like
php cookie 登录验证示例代码
2009/03/16 PHP
判断是否为指定长度内字符串的php函数
2010/02/16 PHP
php使用sql server验证连接数据库的方法
2014/12/25 PHP
Java和PHP在Web开发方面对比分析
2015/03/01 PHP
你不知道的文件上传漏洞php代码分析
2016/09/29 PHP
PHP数组去重的更快实现方式分析
2018/05/09 PHP
Javascript 判断 object 的特定类转载
2007/02/01 Javascript
动态表格Table类的实现
2009/08/26 Javascript
jQuery 选择器理解
2010/03/16 Javascript
jquery 图片 上一张 下一张 链接效果(续篇)
2010/04/20 Javascript
获取内联和链接中的样式(js代码)
2013/04/11 Javascript
jquery中获得元素尺寸和坐标的方法整理
2014/05/18 Javascript
深入理解JavaScript系列(17):面向对象编程之概论详细介绍
2015/03/04 Javascript
JQuery中两个ul标签的li互相移动实现方法
2015/05/18 Javascript
js 判断所选时间(或者当前时间)是否在某一时间段的实现代码
2015/09/05 Javascript
js实现hashtable的赋值、取值、遍历操作实例详解
2016/12/25 Javascript
javascript 显示全局变量与隐式全局变量的区别
2017/02/09 Javascript
使用contextMenu插件实现Bootstrap table弹出右键菜单
2017/02/20 Javascript
webpack使用 babel-loader 转换 ES6代码示例
2017/08/21 Javascript
详解使用WebPack搭建React开发环境
2019/08/06 Javascript
基于javascript处理nginx请求过程详解
2020/07/07 Javascript
vue-quill-editor的使用及个性化定制操作
2020/08/04 Javascript
Python利用公共键如何对字典列表进行排序详解
2018/05/19 Python
python实现flappy bird小游戏
2018/12/24 Python
浅析python内置模块collections
2019/11/15 Python
利用Python实现朋友圈中的九宫格图片效果
2020/09/03 Python
纯CSS3实现地球自转实现代码(图文教程附送源码)
2012/12/26 HTML / CSS
英国二手iPhone、音乐、电影和游戏商店:musicMagpie
2018/10/26 全球购物
KIKO MILANO俄罗斯官网:意大利领先的化妆品和护肤品品牌
2021/01/09 全球购物
sealed修饰符是干什么的
2012/10/23 面试题
体育系毕业生自荐信
2014/06/28 职场文书
项目经理岗位职责
2015/01/31 职场文书
2015年护理工作总结范文
2015/04/03 职场文书
学前教育见习总结
2015/06/23 职场文书
浅谈如何提高PHP代码的质量
2021/05/28 PHP
Nginx报404错误的详细解决方法
2022/07/23 Servers