python操作mysql

之前一直C++连数据库,略显笨重,最近主题就是黑C++,python操作mysql还是比较方便的。

我用的windows32位,直接安装的编译好MySQLdb,下载地址http://www.codegood.com/downloads

import MySQLdb ok就安装成功了。

#coding=utf-8
#!/usr/bin/env python

import MySQLdb

conn = MySQLdb.connect(host='your ip',user='xxx',passwd='***')
cursor = conn.cursor()
count = cursor.execute("""show databases""")
print 'database num:%d' %count
for x in cursor:
    print x
conn.select_db('test')
count = cursor.execute("select * from test")
print 'records num:%d' %count
result = cursor.fetchone()
print result
result = cursor.fetchmany(2)
for r in result:
    print r
cursor.close()

开始遇到DeprecationWarning: the sets module is deprecated这个问题,参考http://blog.sina.com.cn/s/blog_70aa69580100oi41.html

解决办法:
找到Python26\lib\site-packages\MySQLdb下的__init__.py文件
1) 在文件中 "__init__", 注释掉:
from sets import ImmutableSet
class DBAPISet(ImmutableSet):
新增:
class DBAPISet(frozenset):

2) 在文件中"converters.py", 注释掉  from sets importBaseSet, Set 这一句话。

3) 在文件中"converters.py", 修改 "Set" 成为 "set" ( 只有两个地方需要修改):
大概 line 48: return Set([ i for i in s.split(',') if i ]) 改为 return set([ i for i in s.split(',') if i ])

大概 line 128: Set: Set2Str 改为 set: Set2Str

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值