python输出mysql查询最大值_python查询Mysql并输出到文本

学习Python后写的第二个脚本,逻辑有点乱,等以后在优化! #!/usr/bin/env python'''author:wenminCreated on 2013-4-23'''?import MySQLdb?class MySQLHelper: #配置数据库信息并连接 def __init__(self,host="****",user="****",password="****",port=****

学习Python后写的第二个脚本,逻辑有点乱,等以后在优化!

#!/usr/bin/env python

'''

author:wenmin

Created on 2013-4-23

'''

?

import MySQLdb

?

class MySQLHelper:

#配置数据库信息并连接

def __init__(self,host="****",user="****",password="****",port=****,charset="utf8"):

self.host=host

self.user=user

self.password=password

self.port=port

self.charset=charset

try:

self.conn=MySQLdb.connect(host=self.host,user=self.user,passwd=self.password,port=self.port)

self.conn.set_character_set(self.charset)

self.cur=self.conn.cursor()

except MySQLdb.Error as e:

print("Mysql Error %d: %s" % (e.args[0], e.args[1]))

#取出需要统计的数据库名称

def db_name(self):

un_db_name = ['information_schema','cz','ecshop','edutone','gz','mysql','newparent','parent','performance_schema','test','xx','yyhd']

name = []

try:

self.cur.execute('show databases')

for row in self.cur.fetchall():

for i in row:

if i not in un_db_name:

name.append(i)

return name

except MySQLdb.Error as e:

print("Mysql Error %d: %s" % (e.args[0], e.args[1]))

#指定查询的数据库名称

def selectDb(self,db):

try:

self.conn.select_db(db)

except MySQLdb.Error as e:

print("Mysql Error %d: %s" % (e.args[0], e.args[1]))

#查询用户数

def user_quantity(self):

try:

self.cur.execute('select count(distinct phone) from pc_user')

for row in self.cur.fetchall():

return row[0]

except MySQLdb.Error as e:

print("Mysql Error:%s\n" %(e))

#查询用户详细信息

def user_details(self,db):

try:

self.cur.execute('select a.phone,a.parents_name,a.student_name,a.type,c.grade_name,b.class_name,a.sex,"'+str(db)+'"from pc_user a,pc_class b,pc_grade c where a.class_id=b.id and a.grade_id=c.id group by a.phone')

#for row in self.cur.fetchall():

# return row

s = self.cur.fetchall()

return s

except MySQLdb.Error as e:

print("Mysql Error:%s\n" %(e))

#查询议案数

def monion_quantity(self):

try:

self.cur.execute('select count(distinct id) from pc_motions')

for row in self.cur.fetchall():

return row[0]

except MySQLdb.Error as e:

print("Mysql Error:%s\n" %(e))

#查询有效议案

def monion_details(self):

try:

self.cur.execute('select `motion_id`,count(*) from pc_motion_voterec group by motion_id having count(*)>5')

# for row in self.cur.fetchall():

# return row

s = self.cur.fetchall()

return s

except MySQLdb.Error as e:

print("Mysql Error:%s\n" %(e))

?

def close(self):

self.cur.close()

self.conn.close()

?

if __name__ == '__main__':

school_db_name = MySQLHelper()

school = school_db_name.db_name()

for i in school:

file = open('jwh/%s' % i,'w')

file.write("================================================\n")

d_name = str(i)

i = MySQLHelper()

i.selectDb(d_name)

file.write("user_quantity:"+str(i.user_quantity())+"\n")

file.write("================================================\n")

s=i.user_details(d_name)

for p in s:

for m in p:

file.write(str(m)+' ')

file.write("\n")

file.write("================================================\n")

file.write("monion_quantity:"+str(i.monion_quantity())+"\n")

file.write("================================================\n")

l=i.monion_details()

for p in l:

for s in p:

file.write(str(s)+" ")

file.write("\n")

i.close()

file.close()

school_db_name.close()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值