python 操作mysql数据库demo

sudo apt-get install python-mysqldb

#!/usr/bin/env python
#encoding=utf-8
import sys 
import MySQLdb

reload(sys) 
sys.setdefaultencoding('utf-8') 
 
try:
    conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='mysql',port=3306,charset='utf8')
    cur=conn.cursor()
    count=cur.execute("SELECT * FROM  user")
    print 'there has %s rows record' % count

    info = cur.fetchall()
    print info

    cur.close()
    conn.commit()
    conn.close()
except MySQLdb.Error,e:
     print "Mysql Error %d: %s" % (e.args[0], e.args[1])


posted @ 2017-07-11 11:45  yfceshi  阅读(300)  评论(0编辑  收藏  举报