python -从数据库中获取数据并以表格的形式在控制台显示

248 阅读1分钟

方法是导入pandas的库, 使用DataFram制表.

控制台直接显示返回的所有数据库数据:

#show food items
showFood = " select id,name,note,price from foodlist;"
result = cur.execute(showFood)
print("Welcome! We now have",result,"kinds of pizza now. "
      "You could find the index number, name, price and note of our pizzas in the following table."
      "Please feel free to order them! Thank you!")
for row in cur.fetchall():
    print(row)

在这里插入图片描述

使用DataFrame绘制表格:

import pandas as pd

fd = pd.DataFrame(list(cur.fetchall()),columns=["index number","name","note","price"])
print(fd)

在这里插入图片描述