之前一直是在CentOS上使用python操作Mysql数据库,MySQLdb模块使用一直没有问题。为了使用方便,今天想在Windows下安装MySQLdb,却遇到了一系列的问题。现将安装过程中遇到的问题记录如下:
1、首先下载MySQLdb的安装包,这里下载的是:MySQL-python-1.2.3.tar.gz
2、解压后,进入其目录,执行:python setup.py install
D:\MyFile\Frompolo\python\MySQL-python-1.2.3>pythonsetup.py install Traceback (most recent call last): File "D:\MyFile\Frompolo\python\MySQL-python-1.2.3\setup.py",line 5, in <module> from setuptools import setup, Extension ImportError:No module named setuptools |
4、右键保存ez_setup.py文件后,进入该文件所存放的目录下,执行:python ez_setup.py
D:\MyFile\Frompolo\python>python ez_setup.py Downloadinghttps://pypi.python.org/packages/source/s/setuptools/setuptools-15.1.zip Extracting inc:\users\chenhx\appdata\local\temp\tmpavnzc6 Now working inc:\users\chenhx\appdata\local\temp\tmpavnzc6\setuptools-15.1 Installing Setuptools running install running bdist_egg running egg_info ………… Processing setuptools-15.1-py2.6.egg Copying setuptools-15.1-py2.6.egg toc:\python26\lib\site-packages Adding setuptools 15.1 to easy-install.pthfile Installing easy_install-script.py script toC:\Python26\Scripts Installing easy_install.exe script toC:\Python26\Scripts Installing easy_install.exe.manifest scriptto C:\Python26\Scripts Installing easy_install-2.6-script.pyscript to C:\Python26\Scripts Installing easy_install-2.6.exe script toC:\Python26\Scripts Installing easy_install-2.6.exe.manifestscript to C:\Python26\Scripts
Installed c:\python26\lib\site-packages\setuptools-15.1-py2.6.egg Processing dependencies forsetuptools==15.1 Finished processing dependencies forsetuptools==15.1 D:\MyFile\Frompolo\python> |
5、setuptools安装完成后,开始安装MySQLdb模块。
6、进入MySQLdb安装介质的根目录,执行安装命令:
D:\MyFile\Frompolo\python\MySQL-python-1.2.3>setup.py install running install running bdist_egg running egg_info writing MySQL_python.egg-info\PKG-INFO writing top-level names toMySQL_python.egg-info\top_level.txt writing dependency_links toMySQL_python.egg-info\dependency_links.txt reading manifest file'MySQL_python.egg-info\SOURCES.txt' reading manifest template 'MANIFEST.in' warning: no files found matching 'MANIFEST' warning: no files found matching'ChangeLog' warning: no files found matching 'GPL' writing manifest file'MySQL_python.egg-info\SOURCES.txt' installing library code tobuild\bdist.win32\egg running install_lib running build_py copying MySQLdb\release.py ->build\lib.win32-2.6\MySQLdb running build_ext building '_mysql' extension error: Microsoft Visual C++ 9.0 is required(Unable to find vcvarsall.bat). Get it from http://aka.ms/vcpython27 |
7、从错误信息看,还需要安装微软的VisualC++ Compiler。打开错误信息中的URL,竟然是:
由于我使用的是Python 2.6,找来找去却只有 for Python2.7的,没有找到for Python 2.6的。于是就试着把2.7下载并安装。
8、C++ Complier安装完成后,再执行安装命令:
D:\MyFile\Frompolo\python\MySQL-python-1.2.3>setup.py install running install running bdist_egg running egg_info ………… ProcessingMySQL_python-1.2.3-py2.6-win32.egg Copying MySQL_python-1.2.3-py2.6-win32.eggto c:\python26\lib\site-packages Adding MySQL-python 1.2.3 toeasy-install.pth file
Installed c:\python26\lib\site-packages\mysql_python-1.2.3-py2.6-win32.egg Processing dependencies forMySQL-python==1.2.3 Finishedprocessing dependencies for MySQL-python==1.2.3 |
9、这回算是把MySQLdb模块安装上了。
10、进入Python的REPL交互命令行界面进行测试:
D:\MyFile\Frompolo\python\MySQL-python-1.2.3>python Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32)[MSC v.1500 32 bit (Intel)] on win32 Type "help","copyright", "credits" or "license" for moreinformation. >>> import MySQLdb >>> >>>exit() |
可以正常import,模块安装成功。
11、运行脚本往Mysql插入数据时,又遇到了问题:
E:\workdir\projectFile\project\AnHui_project\APP>pythonxmlParse4domain_v0.2.py Traceback (most recent call last): File "xmlParse4domain_v0.2.py", line 93, in <module> cur_i.execute(sql_insert) File "build\bdist.win32\egg\MySQLdb\cursors.py", line 174, inexecute File "build\bdist.win32\egg\MySQLdb\connections.py", line 36,in defaulterrorh andler _mysql_exceptions.OperationalError:(1366, "Incorrect string value: '\\xB0\\xB2\\xD7\\xBFMM' for column'appName' at row 1") |
这个是由字符编码问题引起的,因为往Mysql插入的数据中包含有中文。除了在程序开头写好“#-*- coding: utf-8 -*-”、连接数据库时设置“conn=MySQLdb.connect(host="192.168.10.1",user="root",passwd="123456",db="test",charset="utf8")”外,在进行插入操作前做一下编码转换:
appName = appName.decode("gbk").encode("utf-8") |
PS:网上有说Windows下安装MySQLdb模块,还需要两个dll文件,但我在安装过程并没有遇到该问题,具体请参见如下URL:
http://fc-lamp.blog.163.com/blog/static/17456668720113711510954/