By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,087 Members | 1,508 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,087 IT Pros & Developers. It's quick & easy.

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

P: n/a
Hi,

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'])

It raises this error: AttributeError: 'tuple' object has no attribute
'encode'

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?

Thanks!
Erik

Apr 5 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Apr 5, 10:31 am, "erikcw" <erikwickst...@gmail.comwrote:
Hi,

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'])

It raises this error: AttributeError: 'tuple' object has no attribute
'encode'

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?

Thanks!
Erik
It sounds like you're not calling the "encode" method correctly. But
it's hard to say when you didn't include that bit of code. You may
need to check your database's docs to make sure it accepts unicode
strings and if so, what types (utf-8, 16, 32).

See this post for a similar problem:

http://lists.modevia.com/archives/py...er/001719.html

and this link details tuple usage: http://www.faqs.org/docs/diveintopyt...per_tuple.html

Mike

Apr 5 '07 #2

P: n/a
On Apr 5, 11:41 am, kyoso...@gmail.com wrote:
On Apr 5, 10:31 am, "erikcw" <erikwickst...@gmail.comwrote:
Hi,
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'])
It raises this error: AttributeError: 'tuple' object has no attribute
'encode'
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?
Thanks!
Erik

It sounds like you're not calling the "encode" method correctly. But
it's hard to say when you didn't include that bit of code. You may
need to check your database's docs to make sure it accepts unicode
strings and if so, what types (utf-8, 16, 32).

See this post for a similar problem:

http://lists.modevia.com/archives/py...ecember/001719....

and this link details tuple usage:http://www.faqs.org/docs/diveintopyt...per_tuple.html

Mike
I tried adding .encode("utf-8") onto each of the variables in the
tuple, but that didn't seem to help. The error just changes slightly
to AttributeError: 'long' object has no attribute 'encode'

I'm using Mysql.

Apr 5 '07 #3

P: n/a
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

Apr 5 '07 #4

P: n/a
On Apr 5, 12:37 pm, "Paul Boddie" <p...@boddie.org.ukwrote:
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!

Apr 5 '07 #5

P: n/a
erikcw wrote:
Hi,

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'])
I am guessing you want the string formatting operator here:

sql = """...""" % (cid, ...)

The comma creates a tuple.
Apr 5 '07 #6

P: n/a
Lenard Lindstrom 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'])

I am guessing you want the string formatting operator here:

sql = """...""" % (cid, ...)
That's a superficial solution which encourages a bad practice: if any
of that data can be subverted to modify the query, as opposed to
merely providing a simple value, then you have a vulnerability in your
code. Perhaps the %i and %d substitutions may prevent such things, but
the %s substitution won't.

Paul

Apr 5 '07 #7

P: n/a
erikcw wrote:
>
Here is the full error: (sorry)
No problem! It's easier to comment with these details...
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'
If cid is a long, it won't have an encode method, but this is a
diversion from the real problem.
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)
This won't work because the first parameter of the execute method must
be a string (or Unicode object, I guess), but you've provided a tuple.
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!
The first modification may not have been necessary, but you should
check the database module documentation to make sure what kinds of
parameter markers are permitted. The second modification should, as I
suspected, solve your problem. As you may be aware, adding the *
unpacks the contents of sql into the parameters for the execute
method. Thus, the first element in the tuple gets presented as the
first parameter (the SQL statement), and the second element gets
presented as the second parameter (a sequence of values which are to
be used with the SQL statement).

Paul

Apr 5 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.