1. [更新 2012-09-16]
  2. 这里可以下载已经打包好的EXE文件,http://sourceforge.net/projects/mysql-python/(国内需穿越才可访问)
  3. 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不用多说了,下载下来安装就是,没有特别需要注意的地方(本来是有的,不过有替代方案,见后文)。一个下载地址:

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

安装SetupTools

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

  1. ImportError: No module named setuptools

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

安装MySQL-Python

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

  1. setup.py build
  2. setup.py install

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

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

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

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

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

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

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

  1. 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的目录:

  1. 7
  2. 8
  3. 9
  1. #serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options['registry_key'])
  2. #mysql_root, dummy = _winreg.QueryValueEx(serverKey,'Location')
  3. 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看是否有异常:

  1. import MySQLdb

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

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

    闭包(Closures) 在ECMAScript中,函数是“第一类”对象.这个名词意味着函数可以作为参数被传递给其他函数使用 (在这种情况下,函数被称为“funargs”——“functional a ...

  2. inno setup 脚本常用修改 转

    http://blog.sina.com.cn/s/blog_72c2eb350100y2sa.html 有人提及想更换安装界面的图片,其实方法很简单,只需要修改inno setup安装目录下的Wiz ...

  3. synchronized和lock区别

    synchronized 快速回顾: 1.当代码块 加上 synchrozized之后,代码会发生什么改变? 答案:有两条改变.一个是原子性(atomicity),一个是可见性(visibility) ...

  4. Sqlserver数据库还原.bak文件失败的两个问题

    一.SQL Server数据库备份还原后,在数据库名称后会出现“受限制访问”字样      解决方案:将数据库限制访问改为:SINGLE_USER 数据库-->属性-->选项-->状 ...

  5. python logging模块用法

    http://blog.csdn.net/zyz511919766/article/details/25136485/ import logging logging.debug('debug mess ...

  6. 如何修改chrome谷歌浏览器的默认搜索引擎

    如图设置,chrome自己提供的百度的引擎,不能用,自己添加一个即可 添加的方法如下:打开百度搜索内容“cai”,然后把搜索的url内容放到上图的网址栏里,并用%s替换“cai”

  7. iOS:导航条滚动透明度随着tableView的滚动而变化

    来源:HelloYeah 链接:http://www.jianshu.com/p/b8b70afeda81 下面这个界面有没有觉得很眼熟.打开你手里的App仔细观察,你会发现很多都有实现这个功能.比如 ...

  8. unity linear space时 photoshop blend的正确设置

    gamma correction的dcc设置 ps在线性空间下工作 blend的时候颜色设置 勾选用灰度系数混合rgb 1.0 这样就是在线性空间下工作了 这样素材在数学上是正确的 r8g8b8a8格 ...

  9. decorator在Python中的作用

    decorator(装饰器)在python中作用,可以起到代码复用,也可以起到AOP(面向横切面)的作用. 另外很重要的一点应该就是function在python的世界中是作为一等公民存在的. 在py ...

  10. 阿里巴巴Android开发手册(规约)

    阿里巴巴Android开发手册(规约) 学习了:https://www.cnblogs.com/jb2011/p/8487889.html  这个猛 https://blog.csdn.net/ali ...