#!/usr/bin/env python """[options] Prints out the contents of a database table""" import MySQLdb, sys from optparse import OptionParser parser = OptionParser('usage: %prog ' + __doc__) parser.add_option("-n","--numlines",default=0,dest="num",type="int",help="number of lines to print (default all)") parser.add_option("-r","--raw",action="store_true",dest="raw",help="raw print of data") parser.add_option("-q","--quiet",action="store_true",dest="quiet",help="no title line") (options,args) = parser.parse_args() if len(args) < 1: parser.error("incorrect number of parameters") dbtable=args[0] limit = "" if options.num != 0: limit = " LIMIT " + str(options.num) hostname='stillwaterfs.cfaw.info' dbuser='cfaw' dbpass='xxxx' dbname='cfaw' db=MySQLdb.connect(host=hostname, user=dbuser, passwd=dbpass, db=dbname)#,charset="utf8")#,use_unicode=True) cursor=db.cursor() cursor.execute("SELECT * FROM " + dbtable + limit) # + " WHERE ID1 >= 0 AND ID1 < 100") names = [f[0] for f in cursor.description] #for row in cursor.fetchall(): # for pair in zip(names, row): # print '%s: %s' % pair if not options.quiet: print '\t'.join(names) printformat = '\t'.join(['%s']*len(names)) #contstruct a format string with enough %s's for row in cursor.fetchall(): if options.raw: print row else: #print '\t'.join([field.decode('utf-8') for field in row]) #print "Bjurstr\xc3\xb6m".decode('utf-8') #print '%s\t%s' % (row[2],row[1]) print printformat % row #print insresult % (row[id], emailad[0].strip(), emailad[1].strip(), greeting) #print row[id], row[website],row[people] #print "found:", emailads cursor.close() db.close()