首页 > 数据库技术 > 详细

Python安装MySQLdb并连接MySQL数据库

时间:2014-03-24 07:35:28      阅读:490      评论:0      收藏:0      [点我收藏+]
当然了,前提是你已经安装了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,

然后就可以进行连接测试了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/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.
==================================================================
标题一
内容一
==================================================================
标题二
内容二

Python安装MySQLdb并连接MySQL数据库,布布扣,bubuko.com

Python安装MySQLdb并连接MySQL数据库

原文:http://www.cnblogs.com/harry0906/p/3619776.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!