python链接mysql常见问题汇总
Python与Mysql
一、安装MySQLdb模块
使用python连接Mysql的前提,就是需要一个让python连接到Mysql的接口,这就是MySQLdb模块。
验证是否已经安装了MySQLdb:
==========================================================
d:\usr\local\Python25>python
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] onwin32
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb
==========================================================
如果有类似于上面的"No module named MySQLdb",表明MySQLdb尚未安装或安装的不成功!
MySQL 版本:5.0.67
下载地址:http://dev.mysql.com/downloads/mysql/5.0.html#downloads
下载exe文件并安装
==========================================================
Python 版本:2.5
下载地址:http://www.python.org/download/releases/2.5.4/
下载msi文件并安装
MySQLdb版本: MySQLdb Windows binary for Python 2.5
下载地址:http://biohackers.net/wikiattach/Python2(2e)5/attachments/MySQL-python.exe-1.2.1_p2.win32-py2.5.exe
参见:http://forums.mysql.com/read.php?50,129618,140611#msg-140611
常见问题:
1.无法定位程序输入点 mysql_server_init 于动态链接库 LIBMYSQL.dll 上。
----------------------------------------------------------------
D:\usr\local\Python25>python
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\usr\local\Python25\Lib\site-packages\MySQLdb\__init__.py", line 19, in <module>
import _mysql
ImportError: DLL load failed: 找不到指定的程序。
----------------------------------------------------------------
解决方法:把mysql安装目录的bin\libmySQL.dll文件复制到python安装目录的Lib\site-packages下
==========================================================
Python 版本:2.6
下载地址:http://www.python.org/download/releases/2.6.1/
下载msi文件并安装
MySQLdb版本: MySQL-python-1.2.2.win32-py2.6.exe
下载地址:http://home.netimperia.com/files/misc/MySQL-python-1.2.2.win32-py2.6.exe
参见:http://sourceforge.net/forum/forum.php?thread_id=2316047&forum_id=70460
常见问题:
1.ImportError: DLL load failed: 找不到指定的模块。
----------------------------------------------------------------
D:\usr\local\Python26>python
Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\usr\local\Python26\Lib\site-packages\MySQLdb\__init__.py", line 19, in <module>
import _mysql
ImportError: DLL load failed: 找不到指定的模块。
----------------------------------------------------------------
解决方法:下载libmmd.dll(附件)和libguide40.dll(附件)两个dll文件并复制System32目录之下
参见:http://sourceforge.net/forum/message.php?msg_id=5613887
2.ImportError: DLL load failed: 找不到指定的模块。
----------------------------------------------------------------
D:\usr\local\Python26>python
Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
D:\usr\local\Python26\lib\site-packages\MySQLdb\__init__.py:34: DeprecationWarning: the sets module is deprecated
from sets import ImmutableSet
----------------------------------------------------------------
解决方法:
1) 在文件中 "__init__", 注释掉:
from sets import ImmutableSet
class