[更新 2012-09-16]
这里可以下载已经打包好的EXE文件,http://sourceforge.net/projects/mysql-python/(国内需穿越才可访问)
DBank备份下载地址:http://dl.vmall.com/c0bgsx0s0p (Python 2.7版本 MySQL-python-1.2.3.win32-py2.7.msi)

为了给Python装个MySQLdb模块(这里说的是Windows),真是破费了不少时间。本来Python自带SQLite数据库模块,使用起来也挺方便的,但是SQLite不支持远程访问啊!!!所以只能用MySQL了。下面详细描述一下配置过程,以后可以参考!

安装MySQL

安装MySQL不用多说了,下载下来安装就是,没有特别需要注意的地方(本来是有的,不过有替代方案,见后文)。一个下载地址:

http://xiazai.xiazaiba.com/Soft/M/MySQL_5.5.20_win32_XiaZaiBa.zip

安装SetupTools

下载地址:http://pypi.python.org/pypi/setuptools 如果你不先安装SetupTools而是直接安装MySQLdb,那么很有可能会提示如下错误:

ImportError: No module named setuptools

上面的地址可以直接下载到exe,所以直接执行就是了。

安装MySQL-Python

MySQL-Python也就是MySQLdb了。可以去http://pypi.python.org/pypi/MySQL-python#downloads下载到源码(没有EXE了),解压后,打开cmd来到MySQL-Python的目录,执行如下命令:

setup.py build
setup.py install

如果不出意外的话,会提示如下错误:

E:\Code\Python\mysql>setup.py install
Traceback (most recent call last):
File "E:\Code\Python\mysql\setup.py", line 15, in <module>
metadata, options = get_config()
File "E:\Code\Python\mysql\setup_windows.py", line 7, in get_config
serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options['registry_key'])
WindowsError: [Error 2]

一个可能可行的解决方案:打开setup_windows.py,然后将注册表操作的两行代码注释掉,并添加一行代码:

7
8
9
    #serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options['registry_key'])
#mysql_root, dummy = _winreg.QueryValueEx(serverKey,'Location')
mysql_root = "C:\Program Files\MySQL\MySQL Server 5.5" #MySQL目录

接下来,再执行上面提到的build命令,如果MySQL的版本好的话,就没问题。不过很有可能提示如下错误:

......
_mysql.c(34) : fatal error C1083: 无法打开包括文件:“config-win.h”: No such file or directory
......

网上有人说,重装MySQL并把“C Include Files / Lib Files”勾选上,我这里依然有问题。这里推荐下载MySQL Connector

http://dev.mysql.com/get/Downloads/Connector-C/mysql-connector-c-6.0.2-win32.msi/from/http://ftp.jaist.ac.jp/pub/mysql/

安装好之后,把上面的mysql_root改为MySQL Connector的目录:

7
8
9
    #serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options['registry_key'])
#mysql_root, dummy = _winreg.QueryValueEx(serverKey,'Location')
mysql_root = "C:\Program Files\MySQL\MySQL Connector C 6.0.2" #MySQL Connector C 6.0.2目录

然后再build一般就OK了,接着install即可。

关于重装MySQL

我开始并没有安装MySQL Connector C 6.0.2,而是选择了重装MySQL,也是一个烦人的过程。如果你卸载完MySQL之后重新安装,在MySQL配置的最后一步,很可能提示“ERROR 1045”的一个错误。这是因为上一次的配置文件没有删除。

可以在卸载MySQL后删除Program Files下的MySQL目录,如果是Windows 7,还需要删除C:\ProgramData\MySQL(非常重要),XP一般在C:\Documents and Settings\All Users\Application Data下。

CMySql类

进入cmd,打开python,尝试import看是否有异常:

import MySQLdb

没有异常就说明安装MySQLdb成功了。
下面是我自己简单写的一个CMySql类,可以对MySQL进行简单的增删改查:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python
# -*- coding:utf-8 -*-
 
"""CMySql类,简单的MySQL增删改查
@version: 0.1
@author: 代码疯子
@contect: stackexploit[AT]gmail.com
@see: http://www.programlife.net/
"""
 
try:
import MySQLdb
except ImportError:
raise ImportError("[E]: MySQLdb module not found!")
 
class CMySql(object):
def __init__(self):
self.Option = {"host" : "", "password" : "", "username" : "", "database" : ""}
 
def setoptions(self, host, pwd, user, db):
self.Option["host"] = host
self.Option["password"] = pwd
self.Option["username"] = user
self.Option["database"] = db
 
def start(self):
try:
self.db = MySQLdb.connect(
host = self.Option["host"],
user = self.Option["username"],
passwd = self.Option["password"],
db = self.Option["database"])
self.create()
except Exception, e:
print e
raise Exception("[E] Cannot connect to %s" % self.Option["host"])
 
def create(self, sqlstate):
"""
@todo: sqlstate可以自己改成其他参数,下同
"""
self.cursor = self.db.cursor()
self.cursor.execute(sqlstate) #创建
self.db.commit()
self.cursor.close()
 
def insert(self, sqlstate):
"""
@todo: 虽然函数名是insert,不过增删改都行
"""
self.cursor = self.db.cursor()
self.cursor.execute(sqlstate) #增、删、改
self.db.commit()
self.cursor.close()
 
def query(self, sqlstate):
self.cursor = self.db.cursor()
self.cursor.execute(sqlstate) #查
qres = self.cursor.fetchall()
self.cursor.close()
return qres
 
def one_query(self, sqlstate):
self.cursor = self.db.cursor()
self.cursor.execute(sqlstate) #查
qres = self.cursor.fetchall()[0]
self.cursor.close()
return qres
 
def close(self):
self.db.close()

下载CMySql类

Windows下Python添加MySQLdb扩展模块的更多相关文章

  1. Windows下Python添加库(模块)路径

    动态的添加库路径.在程序运行过程中修改sys.path的值,添加自己的库路径 import syssys.path.append(r'your_path') 在Python安装目录下的\Lib\sit ...

  2. Windows下python安装MySQLdb

    安装MySQLdb需要在电脑上安装MySQL connector C,只需要这个connector就好,不需要把mysql装全. 另外,需要安装VC for python提供编译. 到官网上下载脚本进 ...

  3. Windows下python 安装Mysqldb模块

    CMD执行 pip install mysql-python 报错如下: 1.如果报类似 Microsoft Visual C++ 9.0 is required < Unable to fin ...

  4. windows(32位 64位)下python安装mysqldb模块

    windows(32位 64位)下python安装mysqldb模块 www.111cn.net 编辑:mengchu9 来源:转载 本文章来给各位使用在此windows系统中的python来安装一个 ...

  5. Windows下python的配置

    Windows下python的配置 希望这是最后一次写关于python的配置博客了,已经被python的安装烦的不行了.一开始我希望安装python.手动配置pip并使用pip安装numpy,然而发现 ...

  6. windows下python web开发环境的搭建

    windows下python web开发环境: python2.7,django1.5.1,eclipse4.3.2,pydev3.4.1 一. python环境安装 https://www.pyth ...

  7. Windows下Python读取GRIB数据

    之前写了一篇<基于Python的GRIB数据可视化>的文章,好多博友在评论里问我Windows系统下如何读取GRIB数据,在这里我做一下说明. 一.在Windows下Python为什么无法 ...

  8. [转]Windows下Python多版本共存

    https://blog.csdn.net/dream_an/article/details/51248736 Windows下Python多版本共存 Python数据科学安装Numby,pandas ...

  9. python学习:Windows 下 Python easy_install 的安装

    Windows 下 Python easy_install 的安装     下载安装python安装工具下载地址:http://pypi.python.org/pypi/setuptools 可以找到 ...

随机推荐

  1. 基于 Dapper 的一个 DbUtils

    /// <summary> /// v1.0 /// </summary> public partial class DbUtils { string ConnectionSt ...

  2. 网页中输出漂亮格式的Php数组神器

    写网页的时候经常需要在页面中打印数组,但格式特别难看,看看一个html神器吧<pre>标签,能非常标准的显示数组格式 使用的时候只需要这样打印你的数组就OK了,太好用了,神器! 只需要两句 ...

  3. Microsoft office(2)多级标题的设置

    在Microsoft office中要达到下面的标题结构: 1.首先将文字准备好: 2.将“绪论”,“无线...介绍”等章节标题分别选中 :段落-->大纲级别-->1级 3.同样的,“研究 ...

  4. [Android Memory] Linux下malloc函数和OOM Killer

    http://www.linuxidc.com/Linux/2010-09/28364.htm Linux下malloc函数主要用来在用户空间从heap申请内存,申请成功返回指向所分配内存的指针,申请 ...

  5. OpenShift应用镜像构建(3) - Jenkins的流水线构建

    Jenkins方式构建的定位是使用专门的CICD平台. 既支持把JenKins作为一个Pod部署到openshift内部,也支持部署在Openshift集群外部,操作上的区别是 openshift自己 ...

  6. mysql之事件的开启和调用

    1.检测事件是否开启 mysql> show variables like 'event_scheduler';+-----------------+-------+| Variable_nam ...

  7. es安装脚本

    #!/bin/bash file_name="/sdzw/es5/conf/es.config" #安装目录 install_dir="/es5/esinstall&qu ...

  8. js 在表单提交前进行操作

    最近在写页面的时候,需要手动写一些在表单进行提交前的验证操作,正好看到了2种阻止表单提交的方法,可以进行一些逻辑处理 方法一:使用return false 原生js写法: <form id=&q ...

  9. Spring 自定义配置类bean

    <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.bea ...

  10. OpenCV 4.1 编译和配置

    OpenCV 4.0 版本,历时3年半,终于在2018年圣诞节前发布了,该版本增加的新功能如下: 1) 更新代码支持 c++11 特性,需要兼容 c++11 语法的编译器 2)增加 dnn 中的模块功 ...