Pymysql python打印数据

时间:2016-12-04 15:19:21

标签: python mysql pymysql

cur.fetchall()

我想制作关联数据

((1, '28`c', '30%', datetime.datetime(2016, 12, 4, 8, 16, 50)), (2, '30`c', '87%', datetime.datetime(2016, 12, 4, 8, 16, 58)), (3, '40`c', '8.7%', datetime.datetime(2016, 12, 4, 8, 17, 12)), (4, '20`c', '0.87%', datetime.datetime(2016, 12, 4, 8, 17, 23)), (5, '-8.7`c', '67%', datetime.datetime(2016, 12, 4, 8, 17, 37)))

到此:

+----+-------------+-------+---------------------+ | id | temperature | wet | created | +----+-------------+-------+---------------------+ | 1 | 28`c | 30% | 2016-12-04 08:16:50 | | 2 | 30`c | 87% | 2016-12-04 08:16:58 | | 3 | 40`c | 8.7% | 2016-12-04 08:17:12 | | 4 | 20`c | 0.87% | 2016-12-04 08:17:23 | | 5 | -8.7`c | 67% | 2016-12-04 08:17:37 | +----+-------------+-------+---------------------+

我使用以下内容在cmd(win10)中首先制作基本

for item in cur.fetchall(): print(item)

但它没有显示任何内容。

我怎么修好?

P.S。如果信息不足以回答,请告诉我,thx。

1 个答案:

答案 0 :(得分:0)

您可以使用prettytable包打印漂亮的格式化表格。

在我的示例中,我在创建新的PrettyTable对象时添加标题。只需使用add_row()方法添加行。

import datetime
from prettytable import PrettyTable

data = ((1, '28`c', '30%', datetime.datetime(2016, 12, 4, 8, 16, 50)), (2, '30`c', '87%', datetime.datetime(2016, 12, 4, 8, 16, 58)), (3, '40`c', '8.7%', datetime.datetime(2016, 12, 4, 8, 17, 12)), (4, '20`c', '0.87%', datetime.datetime(2016, 12, 4, 8, 17, 23)), (5, '-8.7`c', '67%', datetime.datetime(2016, 12, 4, 8, 17, 37)))

t = PrettyTable(['id', 'temperature', 'wet', 'created'])
for row in data:
    t.add_row(row)

print(t)

输出:

+----+-------------+-------+---------------------+
| id | temperature |  wet  |       created       |
+----+-------------+-------+---------------------+
| 1  |     28`c    |  30%  | 2016-12-04 08:16:50 |
| 2  |     30`c    |  87%  | 2016-12-04 08:16:58 |
| 3  |     40`c    |  8.7% | 2016-12-04 08:17:12 |
| 4  |     20`c    | 0.87% | 2016-12-04 08:17:23 |
| 5  |    -8.7`c   |  67%  | 2016-12-04 08:17:37 |
+----+-------------+-------+---------------------+