我是在windows下面测试的:
首先要安装MySQL-python-1.2.2.win32-py2.6.exe,如果在linux下则安装linux下面的对应文件,可以再网上下载。
接下来让我们写一段测试代码看看是不是已经可以正确连接了:
1 2 3 4 5 6 7
import MySQLdb db = MySQLdb.connect(user='me', db='mydb', passwd='secret', host='localhost') cursor = db.cursor() cursor.execute('SELECT name FROM books ORDER BY name') names = [row[0] for row in cursor.fetchall()] print names db.close()
如果在import MySQLdb的时候出现下面的提示错误:
1 2 3 4
'deprecation warning': C:\Python\lib\site-packages\MySQLdb\__init__.py:34: DeprecationWarning: the sets module is deprecated from sets import ImmutableSet
那我们试着改写类库文件:
1) 打开 “__init__”, 替换:
from sets import ImmutableSet
class DBAPISet(ImmutableSet):
class DBAPISet(frozenset)
2) 打开”converters.py”, 删除:
from sets import BaseSet, Set
3) 在”converters.py” 文件中把 “Set” 替换成 “set” (IMPORTANT:
有两处需要修改):
line 48: return set([ i for i in
s.split(',') if i ])
line 128: set: Set2Str