为了给Python装个MySQLdb模块(这里说的是Windows),真是破费了不少时间。本来Python自带SQLite数据库模块,使用起来也挺方便的,但是SQLite不支持远程访问啊!!!所以只能用MySQL了。下面详细描述一下配置过程,以后可以参考!
【转载者说明:】【我这里用的版本是python2.6 MySQL5.5】
安装MySQL不用多说了,下载下来安装就是,没有特别需要注意的地方(本来是有的,不过有替代方案,见后文)。一个下载地址:
下载地址:http://pypi.python.org/pypi/setuptools 如果你不先安装SetupTools而是直接安装MySQLdb,那么很有可能会提示如下错误:
上面的地址可以直接下载到exe,所以直接执行就是了。
MySQL-Python也就是MySQLdb了。可以去http://pypi.python.org/pypi/MySQL-python#downloads下载到源码(没有EXE了),解压后,打开cmd来到MySQL-Python的目录,执行如下命令:
如果不出意外的话,会提示如下错误:
一个可能可行的解决方案:打开setup_windows.py,然后将注册表操作的两行代码注释掉,并添加一行代码:
#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 Include Files / Lib Files”勾选上,我这里依然有问题。这里推荐下载MySQL Connector
安装好之后,把上面的mysql_root改为MySQL Connector的目录:
【转载者说明:】【我是直接将Mysql-connnector-c安装到mysql的目录下合并了。合并后要运行MySQLInstanceConfig.exe重新配置一遍。mysql_root的目录不用改,还是MySQL的根目录】
#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又遇到一个问题:】
build\temp.win32-2.6\Release\_mysql.pyd.manifest : general error c1010070: Failed to load and parse the manifest. The system cannot find the file specified. error: command ‘mt.exe’ failed with exit status 31
我开始并没有安装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下。
进入cmd,打开python,尝试import看是否有异常:
没有异常就说明安装MySQLdb成功了。
下面是我自己简单写的一个CMySql类,可以对MySQL进行简单的增删改查:
#!/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扩展模块
作者:代码疯子