Python Forum
[PyQt] [Solved]Help Adding Sql Results in ComboBox
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] [Solved]Help Adding Sql Results in ComboBox
#1
Hello,

I have a Database that stores Categories and their low Quantity Values. I have a SELECT statement that gets all the categories from the database and I'm trying to add the returned results to my combo box.

The only problem is the results are being returned as a tuple and not a string or a list of strings, therefore it's not compatible with the comboBox.

Code Snippet:
#----------------------------------------------------------------------------------------------------
#                                   Values for Categories
#----------------------------------------------------------------------------------------------------
        # self.CategoryInput.addItems(CategoryList)

        #Connect to the Category database
        connection = sqlite3.connect(CategoryDatabase)
        cursor = connection.cursor()
        cursor.execute('''
            SELECT Category From Categories
            ''')
        connection.commit()
        CategoryList = cursor.fetchall()
        #Close the connection
        connection.close()
        print(CategoryList)

        self.CategoryInput.addItems(CategoryList)
Output:
Output:
[('N/A',), ('---',), ('---',), ('---',), ('---',), ('---',), ('---',), ('---',), ('---',), ('---',), ('---',), ('---',), ('---',), ('---',), ('---',), ('---',), ('---',), ('---',), ('---',), ('---',), ('---',)] Traceback (most recent call last): line 215, self.CategoryInput.addItems(CategoryList) TypeError: index 0 has type 'tuple' but 'str' is expected
How do I fix this so I can have all the values printed appear in my CategoryInput ComboBox?

Thanks in advance.
Reply
#2
from itertools import chain
category_tuples = [("N/A",), ("-A-",), ("-B-", "-C-", "-D-"), ("-E-", "-F-")]
cagetory_strings = list(chain.from_iterable(category_tuples))
print(cagetory_strings)
Output:
['N/A', '-A-', '-B-', '-C-', '-D-', '-E-', '-F-']
Reply
#3
Thanks! That worked.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] [Solved]Populate ComboBox with for loop? Extra 3 2,157 Jul-31-2022, 09:01 PM
Last Post: Extra
  [PyQt] [Solved]Display PyQtTable results from A->Z & Z->A Extra 2 1,175 Jul-18-2022, 04:04 PM
Last Post: Extra
  [PyQt] [Solved]Display Search Results in QTable Extra 5 2,477 Jun-29-2022, 10:20 PM
Last Post: Extra
  [PyQt] [Solved]Help Adding results from for loop Extra 2 1,472 Jun-24-2022, 05:01 PM
Last Post: Extra
  [PyQt] How can I sync Combobox index to other combobox index? nickzsche 2 2,403 Jan-03-2022, 12:29 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020