[英]How do I print tables from an SQLite databse in python?
So I have a database of information that I would like to print in a nice table format, held as an SQLite db file.所以我有一个信息数据库,我想以漂亮的表格格式打印,保存为 SQLite db 文件。
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.我见过的唯一打印语句以一种令人困惑的方式打印信息,没有对齐不同实体的属性,没有列标题等。
Procedure that creates the table:创建表的过程:
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
And this is the print procedure:这是打印过程:
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', '货架', '50G',3.4,20.0,4.42,5.303999999999999,3.0,13.26,15.911999999999999,2.6519999999999992,10.2,3.0600000000000005)
('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) ( '咖喱酱', '干燥', '货架', '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) ('三文鱼', '冷藏', '冰箱', '100G', 1.25, 20.0, 1.625, 1.95, 3.0, 4.875, 5.85, 0.97499999999999996, 3.725)
('EDAMAME', 'CHILLED', 'FRIDGE', '100G', 3.0, 19.0, 4.0, 5.0, 3.0, 12.0, 15, 3.0, 9.0, 3.0) ('毛豆', '冷藏', '冰箱', '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?哪些是我的数据库中的信息,但有没有办法将其打印为表格?
Adding column names to your rows using row_factory
is well documented :使用row_factory
将列名添加到您的行中是有据可查的:
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))
You could then use str.rjust
and related functions to print the table, or use a csv.DictWriter
with sys.stdout
as the "file":然后,您可以使用str.rjust
和相关函数来打印表格,或者使用csv.DictWriter
与sys.stdout
作为“文件”:
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.