I wrote the second script after learning Python. The logic is messy and will be optimized later! #! Usrbinenvpythonauthor: wenminCreatedon2013-4-23? ImportMySQLdb? ClassMySQLHelper: # configure the database information and connect def _ init _ (self, host *****, user *****, password *****, port ****
I wrote the second script after learning Python. The logic is messy and will be optimized later! #! /Usr/bin/env python ''' author: wenminCreated on 2013-4-23 '''? Import MySQLdb? Class MySQLHelper: # configure the database information and connect def _ init _ (self, host = "*****", user = "****", password = "*****", port = ****
I wrote the second script after learning Python. The logic is messy and will be optimized later!
#! /Usr/bin/env python ''' author: wenminCreated on 2013-4-23 '''? Import MySQLdb? Class MySQLHelper: # configure the database information and connect 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 () Counter t MySQLdb. error as e: p Rint ("Mysql Error % d: % s" % (e. args [0], e. args [1]) # retrieve the database name def db_name (self): un_db_name = ['information _ scheme', 'cz ', 'ecshop', 'edutone ', 'gz ', 'mysql', 'newparent', 'parent', 'performance _ scheme', 'test', 'XX', 'yyhd'] name = [] try: self.cur.exe cute ('show databases') for row in self. cur. fetchall (): for I in row: if I not in un_db_name: name. append (I) return name doesn't MySQLdb. error as e: print (" Mysql Error % d: % s "% (e. args [0], e. args [1]) # specify the queried database name def selectDb (self, db): try: self. conn. select_db (db) limit t MySQLdb. error as e: print ("Mysql Error % d: % s" % (e. args [0], e. args [1]) # query the number of users def user_quantity (self): try: self.cur.exe cute ('select count (distinct phone) from pc_user ') for row in self. cur. fetchall (): return row [0] Doesn't MySQLdb. error as e: print ("Mysql Error: % s \ n" % (e) # query user details Information def user_details (self, db): try: self.cur.exe cute ('select. phone,. parents_name,. student_name,. type, c. grade_name, B. class_name,. sex, "'+ str (db) +'" from pc_user a, pc_class B, pc_grade c where. class_id = B. id and. grade_id = c. id group by. phone ') # for row in self. cur. fetchall (): # return row s = self. cur. fetchall () return s limit t MySQLdb. error as e: print ("Mysql Error: % s \ n" % (e) # Number of query proposals def moni On_quantity (self): try: self.cur.exe cute ('select count (distinct id) from pc_motions ') for row in self. cur. fetchall (): return row [0] Doesn't MySQLdb. error as e: print ("Mysql Error: % s \ n" % (e) # query valid motion def monion_details (self): try: self.cur.exe cute ('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. fet Chall () return s failed t 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 ()
Original article address: python queries Mysql and outputs it to the text. Thank you for sharing it with the original author.