When I run this code:

sql = "SELECT Name FROM nameDatabase"
self.cursor.execute(sql)
list=self.cursor.fetchall()
print list

I get:

>>> [(u'Joe',), (u'Katie',), (u'Bob',), (u'Ian',)]

However, if I try to make a SingleChoiceDialog with wxpython it does not like that list.

Is there any way to get those values in a so it would look like:

>>>

I appreciate any help or suggestions.

Dani AI

Generated

The output from fetchall() is a list of one-item tuples (for a single selected column), which is why wx.SingleChoiceDialog rejected it. fixed this with an explicit loop and suggested a list comprehension — both are correct. A few cleaner alternatives and best-practice notes follow.

Set a connection-level row factory so sqlite returns plain column values instead of tuples. This avoids any post-processing:

import sqlite3

conn = sqlite3.connect('names.db')
conn.row_factory = lambda cursor, row: row[0]
cur = conn.cursor()
cur.execute("SELECT Name FROM nameDatabase")
names = cur.fetchall()   # names is now a list of strings

If you prefer to keep the default row shape, two lightweight ways to extract the first column without manual indexing are shown below.

from operator import itemgetter
rows = cursor.fetchall()
names = list(map(itemgetter(0), rows))
import itertools
rows = cursor.fetchall()
names = list(itertools.chain.from_iterable(rows))

Practical tips: do not use list as a variable name (it hides the built-in). You do not need to call commit() after a SELECT. Use a context manager (with sqlite3.connect(...) as conn:) so connections always close cleanly. Avoid approaches like zip(*rows)[0] — it raises on empty results; the row_factory, itemgetter, and chain options handle empty result sets safely. Finally, pass the resulting plain names list to your dialog (e.g., the choices argument of wx.SingleChoiceDialog).

I figured it out:

nameList=[]
index=0
sql = "SELECT Name FROM nameDatabase"
self.cursor.execute(sql)
all=self.cursor.fetchall()
while index<len(all):
            nameList.append(all[index][0])
            index=index+1
self.connection.commit()
self.connection.close()

You can do more efficient :

sql = "SELECT Name FROM nameDatabase"
self.cursor.execute(sql)
list=[element[0] for element in self.cursor.fetchall()]
print list
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.