473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Apr 5 '07 #1
7 31684
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: GrelEns | last post by:
hello, i would like if this behaviour can be obtained from python : trap an attributeError from inside a subclassing dict class... (here is a silly examples to explain my question) class...
0
by: Erlend Fuglum | last post by:
I have tried and tried, but cannot figure out the source of the following error: AttributeError: 'module' object has no attribute 'menyHMTL' __doc__ = 'Attribute not found.' __getitem__ =...
3
by: MackS | last post by:
I'm new to Python. In general I manage to understand what is happening when things go wrong. However, the small program I am writing now fails with the following message: AttributeError: ClassA...
2
by: rsd | last post by:
Hi, I'm trying get Samsung YH-920 mp3 player to work with Debian GNU/Linux. To do that I need to run http://www.paul.sladen.org/toys/samsung-yh-925/yh-925-db-0.1.py script, the idea behind the...
12
by: rshepard | last post by:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding the solution in the docs I have here. Data are assembled for writing to a database table. A representative tuple looks...
2
by: Thomas Guettler | last post by:
Hi, how can you list the attributes of an object if you catch an AttributeError? I couldn't find a reference in the exception object, which points to the object. I want to call dir() on...
4
by: black_13 | last post by:
what does this error mean? i am trying to use mark hammonds win32 package. Traceback (most recent call last): File "aui2.py", line 11, in <module> import win32com.client File...
4
by: Nikhil | last post by:
I have recently written a small module. When I import the module, I always get the error only when I do -- Traceback (most recent call last): File "<stdin>", line 1, in <module>
9
by: hajdukzivivjecno | last post by:
I am new user of Python. I would like to change my .shp file that I have astext and it looks something like this (using some loop): to something like this (for every polygon): ((5576309...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.