我在python控制台中使用SQLite3创建了数据库。在主菜单中,我有“显示所有数据”选项。我在python中使用了漂亮的表格。但是问题是,当我不退出程序并且选择了3次相同的选项“显示所有数据”时,每次选择此选项时都会添加所有行。假设我在数据库中有4行,并且每当我选择“显示所有数据”时,它将显示/向表中添加下4行。下面的代码
import sqlite3 as sql
from prettytable import PrettyTable
x = PrettyTable()
def display_all_data():
conn = sql.connect("register.db")
cur = conn.cursor()
cur.execute("SELECT firstname,lastname,date_of_birth,id_number FROM datas ORDER BY lastname")
rec = cur.fetchall()
x.field_names = ["Firstname","Lastname","Date of Birth","ID number"]
for item in rec:
print([x.add_row([rec[1],rec[2],rec[3],rec[4]]))
#outside the for-loop
print(x)
conn.commit()
conn.close()
每次选择函数display_all_datas时,它都会添加相同的4行,但是每次选择此选项时,我都希望显示一次行。