---
title: "【转载】Python连接MySQL数据库"
author: "bzu334fc"
date: "2012-01-19T09:47:50Z"
tags: []
categories: ["非技术"]
slug: "python-mysql"
draft: false
lastmod: "2012-01-19T09:47:50Z"
id: 1357883
url: "https://bzu334fc.iteye.com/blog/1357883"
---

【转载】Python连接MySQL数据库 
2011年08月07日
　　原文地址：Python连接MySQL数据库 
　　文章来自：python - 搜搜关键词订阅
　　
　　
　　
　　Python连接MySQL数据库&nbsp;
　　(2011-08-06&nbsp;18:04:45)
　　　　1.连接的关键是安装MySQLdb模块（下载地址：&nbsp;  http://www.codegood.com/archives/4），我用的Python版本是2.7.2，要下载与Python相对应的版本.
　　&nbsp;
　　　　2.下载好后安装，它会自动检测到计算机Python的安装路径，并自动填写模块解压路径（我的是：D:\Python27\Lib\site-packages\）。2.7版本的就是好，再也不用修改MySQLdb模块下的文件了。
　　&nbsp;
　　3.编写conmysql.py:
　　&nbsp;
　　def&nbsp;getmysqlinfo():&nbsp;
　　&nbsp;&nbsp;import&nbsp;MySQLdb&nbsp;
　　&nbsp;&nbsp;conn&nbsp;=&nbsp;MySQLdb.connect(host="localhost",&nbsp;\&nbsp;
　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user="root",&nbsp;\&nbsp;
　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;passwd="wangxing",&nbsp;\&nbsp;
　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db="test")&nbsp;
　　&nbsp;&nbsp;cursor&nbsp;=&nbsp;conn.cursor()&nbsp;
　　&nbsp;&nbsp;cursor.execute("select&nbsp;*&nbsp;from&nbsp;age")&nbsp;
　　&nbsp;&nbsp;res&nbsp;=&nbsp;cursor.fetchall()&nbsp;
　　&nbsp;&nbsp;print&nbsp;res&nbsp;
　　&nbsp;&nbsp;cursor.close()&nbsp;
　　&nbsp;&nbsp;conn.close()
　　&nbsp;
　　　　4.Python&nbsp;(command&nbsp;line)下运行测试：
　　
　　
　　
　　&nbsp;&nbsp;
　　前一篇：&nbsp;Python&nbsp;遍历目录&nbsp;获得文件属性  
　　原文地址：Python连接MySQL数据库 
　　文章来自：python - 搜搜关键词订阅