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 open读写文件实现脚本
Sep 06 Python
Python3通过Luhn算法快速验证信用卡卡号的方法
May 14 Python
python操作ssh实现服务器日志下载的方法
Jun 03 Python
Python实现随机生成手机号及正则验证手机号的方法
Apr 25 Python
Python实现的堆排序算法示例
Apr 29 Python
pandas中去除指定字符的实例
May 18 Python
Python实现将通信达.day文件读取为DataFrame
Dec 22 Python
python时间序列按频率生成日期的方法
May 14 Python
Python帮你微信头像任意添加装饰别再@微信官方了
Sep 25 Python
找Python安装目录,设置环境路径以及在命令行运行python脚本实例
Mar 09 Python
Django模板获取field的verbose_name实例
May 19 Python
Python list去重且保持原顺序不变的方法
Apr 03 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 编写的 25个游戏脚本
2009/05/11 PHP
php设计模式 Bridge (桥接模式)
2011/06/26 PHP
php二维数组转成字符串示例
2014/02/17 PHP
PHP封装CURL扩展类实例
2015/07/28 PHP
使用PHP处理数据库数据如何将数据返回客户端并显示当前状态
2016/02/16 PHP
PHP使用mysqli同时执行多条sql查询语句的实例
2019/03/22 PHP
php数组指针函数功能及用法示例
2020/02/11 PHP
javascript 获取元素位置的快速方法 getBoundingClientRect()
2009/11/26 Javascript
浅析IE10兼容性问题(frameset的cols属性)
2014/01/03 Javascript
全面解析标签页的切换方式
2016/08/21 Javascript
javascript使用 concat 方法对数组进行合并的方法
2016/09/08 Javascript
bootstrap监听滚动实现头部跟随滚动
2016/11/08 Javascript
js实现小窗口拖拽效果
2016/12/03 Javascript
原生javascript上传图片带进度条【实例分享】
2017/04/06 Javascript
老生常谈javascript的面向对象思想
2017/08/22 Javascript
实例讲解JavaScript预编译流程
2019/01/24 Javascript
javascrit中undefined和null的区别详解
2019/04/07 Javascript
typescript配置alias的详细步骤
2020/08/12 Javascript
vue中重定向redirect:‘/index‘,不显示问题、跳转出错的完美解决
2020/09/28 Javascript
[01:03:41]完美世界DOTA2联赛PWL S3 DLG vs Phoenix 第一场 12.17
2020/12/19 DOTA
介绍Python中几个常用的类方法
2015/04/08 Python
Python实现excel转sqlite的方法
2017/07/17 Python
树莓派使用USB摄像头和motion实现监控
2019/06/22 Python
Django 多表关联 存储 使用方法详解 ManyToManyField save
2019/08/09 Python
python中def是做什么的
2020/06/10 Python
详解Django ORM引发的数据库N+1性能问题
2020/10/12 Python
HTML5 CSS3给网站设计带来出色效果
2009/07/16 HTML / CSS
详解H5 活动页之移动端 REM 布局适配方法
2017/12/07 HTML / CSS
《长江之歌》教学反思
2014/04/17 职场文书
节约用水标语
2014/06/11 职场文书
群众路线个人整改措施
2014/10/24 职场文书
工程资料员岗位职责
2015/04/13 职场文书
最感人的道歉情书
2015/05/12 职场文书
2015年教师见习期工作总结
2015/05/20 职场文书
2016新春团拜会致辞
2015/08/01 职场文书
Spring整合Mybatis的全过程
2021/06/28 Java/Android