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语言中的按位运算符
Nov 26 Python
基于Python实现的扫雷游戏实例代码
Aug 01 Python
python实现指定字符串补全空格的方法
Apr 30 Python
python中的编码知识整理汇总
Jan 26 Python
Ubuntu安装Jupyter Notebook教程
Oct 18 Python
Python基于聚类算法实现密度聚类(DBSCAN)计算【测试可用】
Dec 26 Python
Mac在python3环境下安装virtualwrapper遇到的问题及解决方法
Jul 09 Python
python web框架 django wsgi原理解析
Aug 20 Python
使用python的turtle绘画滑稽脸实例
Nov 21 Python
python爬虫模拟浏览器的两种方法实例分析
Dec 09 Python
在flask中使用python-dotenv+flask-cli自定义命令(推荐)
Jan 05 Python
python如何通过pyqt5实现进度条
Jan 20 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 高手之路(二)
2006/10/09 PHP
PHP使用PHPMailer发送邮件的简单使用方法
2013/11/12 PHP
PHP实现微信小程序用户授权的工具类示例
2019/03/05 PHP
使用JS或jQuery模拟鼠标点击a标签事件代码
2014/03/10 Javascript
简单的js图片轮换代码(js图片轮播)
2014/05/06 Javascript
JavaScript-RegExp对象只能使用一次问题解决方法
2014/06/23 Javascript
给js文件传参数(详解)
2014/07/13 Javascript
jQuery使用fadeout实现元素渐隐效果的方法
2015/03/27 Javascript
JS设置cookie、读取cookie
2016/02/24 Javascript
Bootstrap 粘页脚效果
2016/03/28 Javascript
ui组件之input多选下拉实现方法(带有搜索功能)
2016/07/14 Javascript
jquery文字填写自动高度的实现方法
2016/11/07 Javascript
浅谈sass在vue注意的地方
2017/08/10 Javascript
AngularJS实现的省市二级联动功能示例【可对选项实现增删】
2017/10/26 Javascript
Vue3.0 响应式系统源码逐行分析讲解
2019/10/14 Javascript
[19:26]TNC vs EG (BO3)
2018/06/07 DOTA
python实现socket客户端和服务端简单示例
2014/02/24 Python
python数据预处理之将类别数据转换为数值的方法
2017/07/05 Python
Python实现输出某区间范围内全部素数的方法
2018/05/02 Python
Python 3.6 -win64环境安装PIL模块的教程
2019/06/20 Python
将Python文件打包成.EXE可执行文件的方法
2019/08/11 Python
django序列化serializers过程解析
2019/12/14 Python
python pip安装包出现:Failed building wheel for xxx错误的解决
2019/12/25 Python
CSS3中的弹性布局em运用入门详解 1em等于多少像素
2021/02/08 HTML / CSS
Html5新增标签与样式及让元素水平垂直居中
2019/07/11 HTML / CSS
String s = new String(“xyz”);创建了几个String Object?
2015/08/05 面试题
几道Java和数据库的面试题
2013/05/30 面试题
技术学校毕业生求职信分享
2013/12/02 职场文书
教师岗位职责范本
2013/12/29 职场文书
高中美术教学反思
2014/01/19 职场文书
全民健身日活动方案
2014/01/29 职场文书
保护环境建议书400字
2014/05/13 职场文书
开学典礼策划方案
2014/05/28 职场文书
保护环境的标语
2014/06/09 职场文书
辞职信格式范文
2015/05/13 职场文书
趣味运动会标语口号
2015/12/26 职场文书