(self, db, desc, sql, args = [], fields = [],
                      fieldfmt = {})
return 1
Display multirow query as a table.
Display multirow query as a table.
Display multirow query as a table .
def display_table(self, db, desc, sql, args = [], fields = [],
                      fieldfmt = {}):
        """Display multirow query as a table."""

        self.log.debug("display_table: %s" % skytools.quote_statement(sql, args))
        curs = db.cursor()
        curs.execute(sql, args)
        rows = curs.fetchall()
        db.commit()
        if len(rows) == 0:
            return 0

        if not fields:
            fields = [f[0] for f in curs.description]

        widths = [15] * len(fields)
        for row in rows:
            for i, k in enumerate(fields):
                rlen = row[k] and len(str(row[k])) or 0
                widths[i] = widths[i] > rlen and widths[i] or rlen
        widths = [w + 2 for w in widths]

        fmt = '%%-%ds' * (len(widths) - 1) + '%%s'
        fmt = fmt % tuple(widths[:-1])
        if desc:
            print(desc)
        print(fmt % tuple(fields))
        print(fmt % tuple([ '-' * (w - 2) for w in widths ]))
        #print(fmt % tuple(['-'*15] * len(fields)))
        for row in rows:
            vals = []
            for field in fields:
                val = row[field]
                if field in fieldfmt:
                    val = fieldfmt[field](val)
                vals.append(val)
            print(fmt % tuple(vals))
        print('\n')
        return 1
def display_table ( self , db , desc , sql , args = [ ] , fields = [ ] , fieldfmt = { } ) : self . log . debug ( "display_table: %s" % skytools . quote_statement ( sql , args ) ) curs = db . cursor ( ) curs . execute ( sql , args ) rows = curs . fetchall ( ) db . commit ( ) if len ( rows ) == 0 : return 0 if not fields : fields = [ f [ 0 ] for f in curs . description ] widths = [ 15 ] * len ( fields ) for row in rows : for i , k in enumerate ( fields ) : rlen = row [ k ] and len ( str ( row [ k ] ) ) or 0 widths [ i ] = widths [ i ] > rlen and widths [ i ] or rlen widths = [ w + 2 for w in widths ] fmt = '%%-%ds' * ( len ( widths ) - 1 ) + '%%s' fmt = fmt % tuple ( widths [ : - 1 ] ) if desc : print ( desc ) print ( fmt % tuple ( fields ) ) print ( fmt % tuple ( [ '-' * ( w - 2 ) for w in widths ] ) ) #print(fmt % tuple(['-'*15] * len(fields))) for row in rows : vals = [ ] for field in fields : val = row [ field ] if field in fieldfmt : val = fieldfmt [ field ] ( val ) vals . append ( val ) print ( fmt % tuple ( vals ) ) print ( '\n' ) return 1