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

E

erikcw

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
 
K

kyosohma

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
 
E

erikcw

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.
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
P

Paul Boddie

erikcw said:
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
 
E

erikcw

erikcw said:
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!
 
L

Lenard Lindstrom

erikcw said:
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.
 
P

Paul Boddie

Lenard said:
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
 
P

Paul Boddie

erikcw said:
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
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top