Python安装MySQLdb并连接MySQL数据库

时间:2022-10-12 06:33:01
当然了,前提是你已经安装了Python和MySQL。我的Python是2.6版本的。

Python2.6的“Set”有点兼容性问题,自己照着改一下:

http://sourceforge.net/forum/message.php?msg_id=5808948

1)__init__.py

删除 from sets import ImmutableSet

将 class DBAPISet(ImmutableSet):

改成 class DBAPISet(frozenset)

2) converters.py

删除 from sets import BaseSet, Set

3)converters.py

将 "Set"改成 "set" (仅2处):

line 48: return set([ i for i in s.split(',') if i ])

line 128: set: Set2Str,

然后就可以进行连接测试了:
#!/usr/bin/python
# mysql connection test import MySQLdb
import sys try:
conn = MySQLdb.connect(host = 'localhost', user = 'harryxlb', passwd = 'xlb123',
db = 'boiis', charset = 'utf8')
cur = conn.cursor()
except MySQLdb.Error, ex:
print 'MySQL connect Error!'
print ex.args[0], ex.args[1]
sys.exit()
print 'MySQL connect success!' sql = 'SELECT * FROM bo_blog LIMIT 100'
count = cur.execute(sql)
print count, ' records found.'
results = cur.fetchall()
for r in results:
print '==================================================================\n', r[1]
print r[2]

MySQL connect success!

2 records found.
==================================================================
标题一
内容一
==================================================================
标题二
内容二