库扫描


import MySQLdb
import tableConfig
import getpass
import dbrouterHandler
import sys
import time
import types

class Dbupdate:
    _dbrouterlist = []
    user = ''
    password = ''

    _dbrouter = ''
    _configeFile = ''

    logfile=open('/home/alexli/python/logfile.txt','w')


    def __init__(self, dbrouter, config):
        #self.user = raw_input('user>>>')
        #self.password = getpass.getpass('password>>>')
        self.user = 'root'
        self.password = ''

        self._configeFile = config
        self._dbrouter = dbrouter
        self.getDbrouterlist()

    def getDbrouterlist(self):
        handler = dbrouterHandler.DbrouterHandler(self._dbrouter)
        handler.getDbList()
        self._dbrouterlist = handler.getAttrList()

    def query(self):
        try:
            d_count = 0
            for dbrouter in self._dbrouterlist:

                ip = dbrouter.getIp().encode('utf-8');
                port = dbrouter.getPort().encode('utf-8');
                prefix = dbrouter.getPrefix().encode('utf-8');
                start = dbrouter.getStart().encode('utf-8');
                end = dbrouter.getEnd().encode('utf-8');
                print 'ip='+ip
                print 'prefix_'+prefix
                #print 'start'+start
                try:
                    for r in range(int(start), int(end) + 1):
                        #print str(r)+"r-"
                        if r < 10:
                            db = prefix + '_0' + str(r)
                        else:
                            db = prefix + '_' + str(r)
                        

                        print ip
                        print self.user
                        print self.password
                        print db

                        conn = MySQLdb.Connect(host=ip, user=self.user, passwd=self.password, db=db)
                        
                        cursor = conn.cursor(MySQLdb.cursors.DictCursor)
                        #cursor = conn.cursor()
                        
                        config = tableConfig.TableConfig(self._configeFile)
                        config.readConfigFile()
                        config.getTablePrefix()

                        t_start = config.getStart()
                        t_end = config.getEnd()
                        #print t_start
                        #print t_end    
                        for index in range(int(t_start), int(t_end) + 1):
                            log = ''
                            sql = config.getSql().split('%s')
                            if t_start == '00' and index < 10:
                                t = config.getPrefix() + '_0' + str(index)
                            else:
                                t = config.getPrefix() + '_' + str(index)
                            p = sql[0] + t + sql[1]
                            #print 'p='+p 
                            cursor.execute(p)
                            #conn.commit()
                            rows=cursor.fetchall()
                            for row in rows:
                                    #print row["d_count"]
                                if type(row["userid"]) != types.NoneType:
                                        #d_count += row["d_count"]
                                        log += str(row["userid"]) + ' ' + str(row["col2"])+'\n'
                            print ip + ' of db=>>' + db + ' of table=>>' + t + ' is success'
                            self.logfile.write(log)
                            #print ip + ' of db=>>' + db + ' of table=>>' + t + ' is success'
                        conn.close()
                except MySQLdb.Error,e:
                    print ip + ' of db=>>' + db + ' of table=>>' + '' + ' is failure because :'
                    print "    Error %d:%s" %(e.args[0],e.args[1])

                    #self.logfile.write(self.log)
                    sys.exit(1)
        finally:
                #print "d_count = " + str(d_count) + " at " + time.ctime()

            #f=open('/tmp/result.txt','w')
            #f.write("d_count = " + str(d_count) + " at " + time.ctime())
            #f.close()

            self.logfile.close()


            #print "connection is closed"
            #conn.close()

你可能感兴趣的:(sql,python,F#)