python操作数据库

安装MySQLdb,请访问 http://sourceforge.net/projects/mysql-python


我的是2.7版本的MySQL-python-1.2.4b4.win32-py2.7

直接运行就能安装了

 

import MySQLdb

#连接数据库
db=MySQLdb.connect("localhost","root","","test")

#获取坐标游
cursor=db.cursor()

sql="select * from employee \
    where INCOME > '%d'" %(1000)

try:
    cursor.execute(sql)
    #获取所有记录
    results=cursor.fetchall()
    for row in results:
        fname=row[0]
        lname=row[1]
        age=row[2]
        sex=row[3]
        income=row[4]

    #打印结果
        print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
              (fname,lname,age,sex,income)
except:
    print "Error:unable to fetch data"

#关闭数据库连接
db.close()

 

posted @ 2017-03-30 21:51  安筱雨  阅读(207)  评论(0编辑  收藏  举报