如何在python中从SQLite数据库打印表? [英] How do I print tables from an SQLite databse in python?

查看:451
本文介绍了如何在python中从SQLite数据库打印表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个信息数据库,希望以一种美观的表格格式将其打印为一个SQLite数据库文件。

So I have a database of information that I would like to print in a nice table format, held as an SQLite db file.

我见过的唯一打印语句以一种混乱的方式打印信息,没有对齐不同实体的属性,没有列标题等。

The only print statements I have seen print the information in a confusing manner, not aligning the attributes of different entities, no column headers etc.

创建表的过程:

def create_table():
c.execute('CREATE TABLE IF NOT EXISTS orders ( '    #CREATES TABLE named 'orders':
          'name TEXT, '                             #name
          'type_ TEXT, '                            #type of product
          'location STRING, '                       #location of product
          'amount INTEGER, '                        #'weight' of item, g, kg, ml, cl, l, etc. 
          'wholesale_cost REAL, '                   #wholesale cost
          'tax REAL, '                              #tax %
          'sale_pre_tax REAL, '                     #sale value before tax
          'sale_post_tax REAL, '                    #sale value after tax
          'quantity REAL, '                         #how many sold
          'total_sale_pre_tax REAL, '               #total sales before tax
          'total_sale_post_tax, '                   #total sales after tax
          'total_tax REAL, '                        #total tax in GBP
          'total_wholesale_cost REAL, '             #total wholesale cos
          'profit REAL)')                           #total sale profit

这是打印过程:

def read_from_db():
c.execute ('SELECT * FROM orders ') 
for row in c.fetchall():
    print(row)

执行此操作时,它会打印:

When I execute this it prints:


('NORI','DRY','SHELVES','50G' ,3.4、20.0、4.42、5.303999999999999,
3.0、13.26、15.911999999999999、2.6519999999999992、10.2、3.0600000000000005)

('NORI', 'DRY', 'SHELVES', '50G', 3.4, 20.0, 4.42, 5.303999999999999, 3.0, 13.26, 15.911999999999999, 2.6519999999999992, 10.2, 3.0600000000000005)

('CURRY SAUCE','DRY', '货架','500G',5.65、25.0,
7.34500000000000 1,9.18125,1.0,7.345000000000001,9.18125,1.8362499999999997,5.65,1.6950000000000003)

('CURRY SAUCE', 'DRY', 'SHELVES', '500G', 5.65, 25.0, 7.345000000000001, 9.18125, 1.0, 7.345000000000001, 9.18125, 1.8362499999999997, 5.65, 1.6950000000000003)

('SALMON','CHILLED','FRIDGE','100G',1.25, 20.0、1.625、1.95、3.0,
4.875、5.85、0.9749999999999996、3.75、1.125)

('SALMON', 'CHILLED', 'FRIDGE', '100G', 1.25, 20.0, 1.625, 1.95, 3.0, 4.875, 5.85, 0.9749999999999996, 3.75, 1.125)

('EDAMAME','CHILLED','FRIDGE', '100G',3.0、19.0、4.0、5.0、3.0,
12.0、15、3.0、9.0、3.0)

('EDAMAME', 'CHILLED', 'FRIDGE', '100G', 3.0, 19.0, 4.0, 5.0, 3.0, 12.0, 15, 3.0, 9.0, 3.0)

这是我数据库中的信息,但是有什么方法可以将其打印为表格?

Which is the information from my database but is there any way to print this as a table instead?

推荐答案

将列名添加到您使用 row_factory 的行是有据可查的

Adding column names to your rows using row_factory is well documented:

import sqlite3

con = sqlite3.Connection('my.db')
con.row_factory = sqlite3.Row

cur = con.cursor()
cur.execute('SELECT * FROM tbl')

for row in cur.fetchall():
     # can convert to dict if you want:
     print(dict(row))

然后可以使用 str.rjust 和相关函数来打印表,或者将 csv.DictWriter sys.stdout 作为文件:

You could then use str.rjust and related functions to print the table, or use a csv.DictWriter with sys.stdout as the "file":

import csv
import sys

wtr = csv.DictWriter(sys.stdout, fieldnames=[i[0] for i in cur.description])
wtr.writeheader()

for row in cur.fetchall():
    wtr.writerow(dict(row))

这篇关于如何在python中从SQLite数据库打印表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆