problem with appending to a list, possibly mysqldb related

J

John Salerno

Here's the code:

class MyFrame(wx.Frame):

def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY)
panel = wx.Panel(self)
dbconn = self.connect_db()
dbconn.execute("select namefirst, namelast from master")
bb_players = []

for x in dbconn.fetchall():
bb_players.append(' '.join(x))

cb = PromptingComboBox(panel, 'Choose A Player',
choices=bb_players, style=wx.CB_SORT)

def connect_db(self):
connection = MySQLdb.connect('localhost', 'john', '*******',
'bbdatabank')
return connection.cursor()


The error seems to be in the for loop. I get this:

Traceback (most recent call last):
File "C:\Python24\myscripts\bbdata_access\bbdata.py", line 71, in
-toplevel-
app = MyApp(False)
File "C:\Python24\Lib\site-packages\wx-2.6-msw-ansi\wx\_core.py",
line 7700, in __init__
self._BootstrapApp()
File "C:\Python24\Lib\site-packages\wx-2.6-msw-ansi\wx\_core.py",
line 7352, in _BootstrapApp
return _core_.PyApp__BootstrapApp(*args, **kwargs)
File "C:\Python24\myscripts\bbdata_access\bbdata.py", line 64, in OnInit
frame = MyFrame()
File "C:\Python24\myscripts\bbdata_access\bbdata.py", line 15, in
__init__
name = ' '.join(x)
TypeError: sequence item 0: expected string, NoneType found
I know the method of joining the items in the tuple and appending to a
list work, so I figure it has something to do with either mysqldb, or
maybe the PromptingComboBox I am using.

Hope someone can spot the problem!

Thanks,
John
 
J

John Purser

Here's the code:

class MyFrame(wx.Frame):

def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY)
panel = wx.Panel(self)
dbconn = self.connect_db()
dbconn.execute("select namefirst, namelast from master")
bb_players = []

for x in dbconn.fetchall():
bb_players.append(' '.join(x))

cb = PromptingComboBox(panel, 'Choose A Player',
choices=bb_players, style=wx.CB_SORT)

def connect_db(self):
connection = MySQLdb.connect('localhost', 'john', '*******',
'bbdatabank')
return connection.cursor()


The error seems to be in the for loop. I get this:

Traceback (most recent call last):
File "C:\Python24\myscripts\bbdata_access\bbdata.py", line 71, in
-toplevel-
app = MyApp(False)
File "C:\Python24\Lib\site-packages\wx-2.6-msw-ansi\wx\_core.py",
line 7700, in __init__
self._BootstrapApp()
File "C:\Python24\Lib\site-packages\wx-2.6-msw-ansi\wx\_core.py",
line 7352, in _BootstrapApp
return _core_.PyApp__BootstrapApp(*args, **kwargs)
File "C:\Python24\myscripts\bbdata_access\bbdata.py", line 64, in
OnInit frame = MyFrame()
File "C:\Python24\myscripts\bbdata_access\bbdata.py", line 15, in
__init__
name = ' '.join(x)
TypeError: sequence item 0: expected string, NoneType found

John,

I'd say you had a record with a null value for the namefirst field.
The join method don't like that.

John Purser
 
J

John Salerno

John said:
I'd say you had a record with a null value for the namefirst field.
The join method don't like that.

Wow, you're right! I tried this:

if x[0] and not x[0] == 'NULL':

and sure enough it works after that. (Not sure if that's the best way to
test, though. Just testing for NULL still produced the error, so I
guessed that some namefirst fields were actually blank.)
 
J

John Salerno

John said:
John said:
I'd say you had a record with a null value for the namefirst field.
The join method don't like that.

Wow, you're right! I tried this:

if x[0] and not x[0] == 'NULL':

and sure enough it works after that. (Not sure if that's the best way to
test, though. Just testing for NULL still produced the error, so I
guessed that some namefirst fields were actually blank.)

Just tried again with:

if x[0]:

and that worked, so I guess NULL isn't a string value.
 
J

John Purser

John said:
John said:
I'd say you had a record with a null value for the namefirst field.
The join method don't like that.

Wow, you're right! I tried this:

if x[0] and not x[0] == 'NULL':

and sure enough it works after that. (Not sure if that's the best
way to test, though. Just testing for NULL still produced the
error, so I guessed that some namefirst fields were actually blank.)

Just tried again with:

if x[0]:

and that worked, so I guess NULL isn't a string value.
Your original python error was telling you that .join() didn't like
receiving a 'NoneType'. Try entering type(None) from the python prompt.

By the time Python is seeing it it's not a MySQL null, it's a python
None.
 
J

John Salerno

John said:
John said:
John Purser wrote:

I'd say you had a record with a null value for the namefirst field.
The join method don't like that.
Wow, you're right! I tried this:

if x[0] and not x[0] == 'NULL':

and sure enough it works after that. (Not sure if that's the best
way to test, though. Just testing for NULL still produced the
error, so I guessed that some namefirst fields were actually blank.)
Just tried again with:

if x[0]:

and that worked, so I guess NULL isn't a string value.
Your original python error was telling you that .join() didn't like
receiving a 'NoneType'. Try entering type(None) from the python prompt.

By the time Python is seeing it it's not a MySQL null, it's a python
None.

Well, that makes sense! I guess it just didn't occur to me that some
entries would have no first name, but I was ignoring all the obvious
signs! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top