AttributeError: 'tuple' object has no attribute 'encode'

erikcw erikwickstrom at gmail.com
Thu Apr 5 13:11:46 EDT 2007


On Apr 5, 12:37 pm, "Paul Boddie" <p... at boddie.org.uk> wrote:
> erikcw wrote:
>
> > I'm trying to build a SQL string
>
> > sql = """INSERT INTO ag ('cid', 'ag', 'test') VALUES(%i, %s, %d)""",
> > (cid, ag, self.data[parent][child]['results']['test'])
>
> This makes a tuple, though: the first element is the SQL string; the
> second element contains a tuple of parameters.
>
> > It raises this error: AttributeError: 'tuple' object has no attribute
> > 'encode'
>
> What does? I imagine that this error comes from a call to a cursor
> object's execute method. In other words, I imagine that you may be
> doing something like this:
>
> cursor.execute(*sql)
>
> Not that there would be anything obviously wrong with that: you are
> keeping the string and its parameters separate, after all. However,
> you'd have to show us the full error (a traceback including lines of
> code from the database library) for us to really see what's going on.
>
> > Some of the variables are unicode (test and ag) - is that what is
> > causing this error?  What do I need to do to make it work?
>
> Show us more of the error! ;-)
>
> Paul

Here is the full error: (sorry)

Traceback (most recent call last):
 File "/home/erik/Desktop/wa.py", line 178, in ?
curHandler.walkData()
 File "/home/erik/Desktop/wa.py", line 91, in walkData
sql = """INSERT INTO ag ('cid', 'ag', 'test') VALUES(%i, %s, %d)""",
(cid.encode("utf-8"), ag, self.data[parent][child]['results']['test'])
AttributeError: 'long' object has no attribute 'encode'

sql = """INSERT INTO ag ('cid', 'ag', 'test') VALUES(%i, %s, %d)""",
(cid.encode("utf-8"), ag, self.data[parent][child]['results']['test'])
self.cursor.execute(sql)

Now, I changed all ofth e %i/%d to %s, and changed
self.cursor.execute(sql) to self.cursor.execute(*sql) and it seems to
be working now!




More information about the Python-list mailing list