[英]Dump info from database
I'm new to Python, but I'm trying to dump certain info from a database. 我是Python的新手,但是我试图从数据库中转储某些信息。 I currently have the following:
我目前有以下内容:
import os,MySQLdb
db = MySQLdb.connect(read_default_file="/etc/my.cnf",read_default_group="mysql",db="DbName")
cursor = db.cursor()
sql = "select * from DatasetStatus"
try:
print '\n Mysql> ' + sql
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
incache = row[3]
size = row[5]
print "%s"% \
(size)
except:
print " Error ($s): unable to fetch data."%(sql)
db.close()
I need to access print "%s"
, the size of the datasets, but only for those where "%i"
is equal to 1 (meaning it is currently cached). 我需要访问print
"%s"
,即数据集的大小,但仅适用于那些"%i"
等于1(表示当前已缓存)的数据集。 Any thoughts on how I might approach this? 关于如何处理这个问题有任何想法吗?
If I understand your question correctly, your loop should rather be: 如果我正确理解您的问题,则您的循环应该是:
for row in results:
incache = row[3]
if incache == 1:
size = row[5]
print size
ie with the print
inside the loop and guarded by an if
(I simplified away the baroque unneeded structure of the print
itself, because this simplest-of-all-prints does exactly the same thing as your convoluted one, and Occam's Razor roolz -- but, that's independent from your question:-). 也就是说,将
print
置于循环中并用if
(我简化了print
本身不需要的巴洛克式结构,因为这种最简单的打印品的作用与您费解的打印品以及Occam的Razor roolz完全相同-但是,这与您的问题无关:-)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.