f



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

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

1
4/5/2007 3:31:35 PM
comp.lang.python 77058 articles. 6 followers. Post Follow

8 Replies
2700 Views

Similar Articles

[PageSpeed] 21

On Apr 5, 10:31 am, "erikcw" <erikwickst...@gmail.com> 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'])
>
> 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-transports/2005-December/001719.html

and this link details tuple usage: http://www.faqs.org/docs/diveintopython/odbchelper_tuple.html

Mike

0
kyosohma (1259)
4/5/2007 3:41:37 PM
On Apr 5, 11:41 am, kyoso...@gmail.com wrote:
> On Apr 5, 10:31 am, "erikcw" <erikwickst...@gmail.com> 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'])
>
> > 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-transports/2005-December/001719....
>
> and this link details tuple usage:http://www.faqs.org/docs/diveintopython/odbchelper_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.

0
4/5/2007 4:01:14 PM
On 5 Apr 2007 08:31:35 -0700, "erikcw" <erikwickstrom@gmail.com>
declaimed the following in comp.lang.python:

> 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'])
> 
	Is that a cut&paste from the actual code?

	If so, it won't work regardless (and I'm not commenting on what I
presume is Usenet line wrapping).

	Where is the .execute() statement?


sql = "insert into ag (cid, ag, test) values (%s, %s, %s)"
ret = cursor.execute(sql, (cid, ag, self.data[...])

	MySQLdb uses (by documentation) %s for parameter substitution --
whilst said substitution IS done at the Python level using %
interpolation and, thereby, MIGHT work with %i and %d, that requires
ensuring that arguments never trigger a guarded translation. %s is safe
for all.

	The parameters have to be supplied as a second argument to
..execute() -- your statement above is creating a tuple "sql" consisting
of a string (the insert template) AND a tuple of parameters; I'm
guessing you are passing just one argument to .execute(). That argument
is a tuple that can't be "encoded" into a valid SQL statement.
-- 
	Wulfraed	Dennis Lee Bieber		KD6MOG
	wlfraed@ix.netcom.com		wulfraed@bestiaria.com
		HTTP://wlfraed.home.netcom.com/
	(Bestiaria Support Staff:		web-asst@bestiaria.com)
		HTTP://www.bestiaria.com/
0
wlfraed (4596)
4/5/2007 4:32:27 PM
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

0
paul338 (1044)
4/5/2007 4:37:44 PM
On Apr 5, 12:37 pm, "Paul Boddie" <p...@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!

0
4/5/2007 5:11:46 PM
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.


0
len-l (31)
4/5/2007 5:12:38 PM
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

0
paul338 (1044)
4/5/2007 5:55:22 PM
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

0
paul338 (1044)
4/5/2007 6:04:01 PM
Reply: