Search code examples
pythonprettytable

How to remove duplicating rows from python prettytable?


I use the Following code to enter data from mysql database and display it in python shell. I used prettytable to make look pretty. But now I'm stuck with this bug where the rows from prettytable keep repeating itself if I call the display function more than once in a single run. Please Help Me!!!

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()

When I run the above program, I get duplicating rows if I call the displayAll Function more than once! following is the output of the above code__

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  | [email protected] |
+-------+-------+-------------+---------+---------------+

#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  | [email protected] |
| AYUSH |   1   |    123456   |   1234  | [email protected] |
+-------+-------+-------------+---------+---------------+
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  | [email protected] |
| AYUSH |   1   |    123456   |   1234  | [email protected] |
| AYUSH |   1   |    123456   |   1234  | [email protected] |
+-------+-------+-------------+---------+---------------+
To go back to main menu,press any key+enter (or) just enter:'''

Solution

  • A simple fix is to create the table inside 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)
    

    EDIT: even simpler would be:

    from prettytable import from_db_cursor
    
    def displayAll():
        mycursor.execute("select * from accountsxyz")
        print(from_db_cursor(mycursor))