python 操作mysql数据库demo

时间:2024-06-07 09:36:56

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])