Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1695052
  • 博文数量: 362
  • 博客积分: 10587
  • 博客等级: 上将
  • 技术积分: 4098
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-10 18:15
文章分类

全部博文(362)

文章存档

2014年(1)

2013年(58)

2011年(115)

2010年(112)

2009年(76)

分类: Python/Ruby

2011-05-09 15:38:34


如果你不确定你的python环境里有没有这个库,那就打开python shell,输入 import MySQLdb,如果返回错误信息,那就表示你的机器上没有,赶紧去下载一个安装.

  $ tar xfz MySQL-python-1.2.1.tar.gz
  $ cd MySQL-python-1.2.1
  $ # edit site.cfg if necessary
  $ python setup.py build
  $ sudo python setup.py install # or su first



首先下载 MySQLdb
#encoding=GBK
import MySQLdb
#import sys
#
#reload(sys)
#sys.setdefaultencoding('utf-8')
 
print 'Connection ...'
host='192.168.1.77'
user='root'
passwd='123456'
db='test'
conn = MySQLdb.connect(host,user,passwd,db,charset='gbk')
print 'Connection success'
cursor = conn.cursor()
#query = "insert into test(id,name) values(%s , %s)"
#param = ("1","汉字")
#cursor.execute(query,param)
#
#conn.commit()
cursor.execute('select * from test')
rows = cursor.fetchall()
for row in rows:
    print row[1]
cursor.close()
conn.close()

阅读(721) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~