简体   繁体   English

从数据库中转储信息

[英]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.

相关问题 如何从泡菜中转储,加载和读取信息 - How to Dump, Load and Read info from pickle 从Wikipedia数据库转储生成纯文本 - Generating plain text from a Wikipedia database dump 我将如何从链接列表中获取信息,然后将其转储到JSON对象中? - How would I go about getting info from a list of link then dump them into a JSON object? 从 Python 中的存储加载数据库转储的最快方法 - Fastest way to load a database dump from storage in Python 将数据从HTML转储到Python Django中的数据库(SQLite3) - Dump data from HTML to database(SQLite3) in Python Django 通过Django从托管在其他服务器中的数据库中获取Postgres转储 - Get Postgres dump from a database hosted in a different server through Django 解析数据库中的文件并将信息添加到字典中 - Parsing through a file from database and adding info to dictionary 如何使用 Python 将 HTML 从 info 传递到 MySQL 数据库? - How to pass HTML from info to MySQL database using Python? 在 Eve 的预置回调中从数据库中获取项目的信息 - Get info of an item from database in pre-put callback in Eve 合并来自主数据库的其他列信息以获取一些选择性记录 - Merging additional column info from the main database for some selective records
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM