0

我使用以下代码从 mysql 数据库中输入数据并将其显示在 python shell 中。我用了漂亮的表来使看起来漂亮。但是现在我遇到了这个错误,如果我在一次运行中多次调用 display 函数,那么来自 prettytable 的行会不断重复。请帮我!!!

import mysql.connector

from prettytable import PrettyTable

x = PrettyTable()

x.field_names = ["Name","AccNo","PhoneNumber","Deposit","Gmail"]

def displayAll():
    mycursor.execute("select * from accountsxyz")
    for i in mycursor:
        x.add_row(i)
    print(x)

if ch == '5':
    
        displayAll()

当我运行上面的程序时,如果我多次调用 displayAll 函数,我会得到重复的行!以下是上述代码的输出__

OUTPUT:::::----

#calling the displayALL function 1st time ::

'''To go back to main menu,press any key+enter (or) just enter:

    MAIN MENU
    1. NEW ACCOUNT
    2. DEPOSIT AMOUNT
    3. WITHDRAW AMOUNT
    4. BALANCE ENQUIRY
    5. ALL ACCOUNT HOLDER LIST
    6. CLOSE AN ACCOUNT
    7. MODIFY AN ACCOUNT
    8. EXIT
    Select Your Option (1-8) 
5
+-------+-------+-------------+---------+---------------+
|  Name | AccNo | PhoneNumber | Deposit |     Gmail     |
+-------+-------+-------------+---------+---------------+
| AYUSH |   1   |    123456   |   1234  | AQS@GMAIL.COM |
+-------+-------+-------------+---------+---------------+

#calling the displayALL function 2nd time in the same run ::

To go back to main menu,press any key+enter (or) just enter:

    MAIN MENU
    1. NEW ACCOUNT
    2. DEPOSIT AMOUNT
    3. WITHDRAW AMOUNT
    4. BALANCE ENQUIRY
    5. ALL ACCOUNT HOLDER LIST
    6. CLOSE AN ACCOUNT
    7. MODIFY AN ACCOUNT
    8. EXIT
    Select Your Option (1-8) 
5
+-------+-------+-------------+---------+---------------+
|  Name | AccNo | PhoneNumber | Deposit |     Gmail     |
+-------+-------+-------------+---------+---------------+
| AYUSH |   1   |    123456   |   1234  | AQS@GMAIL.COM |
| AYUSH |   1   |    123456   |   1234  | AQS@GMAIL.COM |
+-------+-------+-------------+---------+---------------+
To go back to main menu,press any key+enter (or) just enter:5

MAIN MENU
1. NEW ACCOUNT
2. DEPOSIT AMOUNT
3. WITHDRAW AMOUNT
4. BALANCE ENQUIRY
5. ALL ACCOUNT HOLDER LIST
6. CLOSE AN ACCOUNT
7. MODIFY AN ACCOUNT
8. EXIT
Select Your Option (1-8) 

5
+-------+-------+-------------+---------+---------------+
|  Name | AccNo | PhoneNumber | Deposit |     Gmail     |
+-------+-------+-------------+---------+---------------+
| AYUSH |   1   |    123456   |   1234  | AQS@GMAIL.COM |
| AYUSH |   1   |    123456   |   1234  | AQS@GMAIL.COM |
| AYUSH |   1   |    123456   |   1234  | AQS@GMAIL.COM |
+-------+-------+-------------+---------+---------------+
To go back to main menu,press any key+enter (or) just enter:'''
4

1 回答 1

1

一个简单的解决方法是在里面创建表displayAll

def displayAll():
    x = PrettyTable()
    x.field_names = ["Name","AccNo","PhoneNumber","Deposit","Gmail"]

    mycursor.execute("select * from accountsxyz")
    for i in mycursor:
        x.add_row(i)
    print(x)

编辑:更简单的是:

from prettytable import from_db_cursor

def displayAll():
    mycursor.execute("select * from accountsxyz")
    print(from_db_cursor(mycursor))
于 2021-01-21T10:25:59.030 回答