def queryDb(db='Pawsey', limit=None, saveToCSV=None):
"""
Return the query result as a numpy array
saveToCSV: csv file name (string)
"""
if (not DB.has_key(db)):
raise Exception("Unknown db name {0}".format(db))
# don't want to use group by to relief the database
if (limit is None or len(limit) == 0):
sl = ""
else:
sl = " {0}".format(limit)
hsql = "select file_size from ngas_files {0}".format(sl)
try:
t = dbpass
except NameError:
dbpass = getpass.getpass('%s DB password: ' % db)
print "Connecting to DB"
dbconn = dbdrv.connect(database="ngas", user="ngas_ro",
password=dbpass, host=DB[db])
cur = dbconn.cursor()
print "Executing query '{0}'".format(hsql)
cur.execute(hsql)
r = cur.fetchall()
dbconn.close()
del(dbconn)
res = pylab.array(r)
ll = len(res)
res = res[:,0].reshape(ll)
if (not (saveToCSV is None)):
f = open(saveToCSV, 'wb')
for li in res:
#print "--{0}:{1}".format(int(li), type(int(li)))
f.write("{0}\n".format(li))
return res # get the first (only) column from all rows
评论列表
文章目录