日期:2014-05-16  浏览次数:20784 次

[2010-05-31]Python 2.6连接mysql
突然想起了世界上最优雅的语言python

(why 最优雅,因为强制缩进,看起来的确比较好看)

1,先装MySQLdb库,官方下载的不行,搜到这个

http://www.codegood.com/archives/4

2,遇到__init__.py什么什么的错误,

神级帖子save me http://www.phpfans.net/article/htmls/200907/MjE1MjQ5.html

找到MySQLdb文件夹的中__init__.py,注释掉from sets import ImmutableSet
class DBAPISet(ImmutableSet):添加class DBAPISet(frozenset):;找到converters.py注释掉from sets import BaseSet, Set。然后修改第45行和129行中的Set为set。

3,开始读取book数据库
import MySQLdb
conn=MySQLdb.connect(host="localhost",user="root",passwd="744411",db="book")
cursor=conn.cursor()
cursor.execute("select * from book")
res=cursor.fetchall()
print res
cursor.close()
conn.close()