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 命令行非阻塞输入的小例子
Sep 27 Python
python3.3实现乘法表示例
Feb 07 Python
Python里隐藏的“禅”
Jun 16 Python
用Python计算三角函数之atan()方法的使用
May 15 Python
Python中的下划线详解
Jun 24 Python
Python selenium如何设置等待时间
Sep 15 Python
python读取有密码的zip压缩文件实例
Feb 08 Python
Django框架自定义session处理操作示例
May 27 Python
Python Django 命名空间模式的实现
Aug 09 Python
python利用JMeter测试Tornado的多线程
Jan 12 Python
Python图像处理库PIL的ImageDraw模块介绍详解
Feb 26 Python
Python读取ini配置文件传参的简单示例
Jan 05 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
MVC模式的PHP实现
2006/10/09 PHP
使用js实现按钮控制文本框加1减1应用于小时+分钟
2013/12/09 Javascript
javascript的document.referrer浏览器支持、失效情况总结
2014/07/18 Javascript
Javascript前端UI框架Kit使用指南之kitjs的对话框组件
2014/11/28 Javascript
JS动态添加Table的TR,TD实现方法
2015/01/28 Javascript
js实现鼠标感应图片展示的方法
2015/02/27 Javascript
JavaScript跨浏览器获取页面中相同class节点的方法
2015/03/03 Javascript
把多个JavaScript函数绑定到onload事件处理函数上的方法
2016/09/04 Javascript
使用ESLint禁止项目导入特定模块的方法步骤
2019/03/04 Javascript
基于javascript的拖拽类封装详解
2019/04/19 Javascript
微信小程序wx.request拦截器使用详解
2019/07/09 Javascript
vue实现手机号码的校验实例代码(防抖函数的应用场景)
2019/09/05 Javascript
node.js Promise对象的使用方法实例分析
2019/12/26 Javascript
JS运算符简单用法示例
2020/01/19 Javascript
非常漂亮的js烟花效果
2020/03/10 Javascript
[01:59]翻天覆地,因你而变,7.20版本地图更新速览
2018/11/24 DOTA
python交互式图形编程实例(三)
2017/11/17 Python
Python反射用法实例简析
2017/12/22 Python
python基于twisted框架编写简单聊天室
2018/01/02 Python
Python提取特定时间段内数据的方法实例
2019/04/01 Python
selenium 多窗口切换的实现(windows)
2020/01/18 Python
解决python 找不到module的问题
2020/02/12 Python
scrapy数据存储在mysql数据库的两种方式(同步和异步)
2020/02/18 Python
python实现梯度下降和逻辑回归
2020/03/24 Python
使用keras和tensorflow保存为可部署的pb格式
2020/05/25 Python
世界最大域名注册商:GoDaddy
2016/07/24 全球购物
德国足球商店:OUTFITTER
2019/05/06 全球购物
造价工程师个人求职信
2013/09/21 职场文书
教育专业个人求职信
2013/12/02 职场文书
父母寄语大全
2014/04/12 职场文书
五四青年节演讲稿
2014/05/26 职场文书
公司年底活动方案
2014/08/17 职场文书
个人债务授权委托书
2014/10/17 职场文书
李强为自己工作观后感
2015/06/11 职场文书
浅谈Python列表嵌套字典转化的问题
2021/04/07 Python
MySQL中的隐藏列的具体查看
2021/09/04 MySQL