0

I am trying to get country names from database and paste them to QComboBox. Everything working fine except that tuples coming out of db table like ('Afghanistan',) , ('Angola',) ... The question is how to delete that unuseful characters from tuples. The code is:

countries = []
try:
    conn = MySQLdb.connect(host='localhost', user='root', passwd='*****', db='world')
    cursor = conn.cursor()
    try:
        cursor.execute("SELECT Name from country")
        while True:
            rows = cursor.fetchone()
            if rows == None:
                break
            else:
                countries.append(rows)
    except:
        print('Error CURSOR')
    cursor.close()
    conn.close()
except:
    pass
for i in countries:
    self.country_cbox.addItem(str(i)) # Converting to string
print(countries)
self.add_worker.show()
self.add_worker.exec_()

enter image description here

5
  • 1
    Try countries.append(rows.Name) or rows[0] Commented Feb 28, 2017 at 17:06
  • Thanks man. My brain is tired i must take some coffee :) Commented Feb 28, 2017 at 17:11
  • Good idea Enjoy it ;) Commented Feb 28, 2017 at 17:12
  • @metmirr Can you post your comment as an answer? Thanks! Commented Oct 29, 2018 at 17:51
  • @alex Posted it. Commented Oct 29, 2018 at 18:03

1 Answer 1

1

You can use column name or tuple index to delete value:

countries.append(rows.Name)  # column name
# or use 
rows[0]  # column index
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.