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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • erikcw

    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

  • kyosohma@gmail.com

    #2
    Re: AttributeError: 'tuple' object has no attribute 'encode'

    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:



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

    Mike

    Comment

    • erikcw

      #3
      Re: AttributeError: 'tuple' object has no attribute 'encode'

      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.

      Comment

      • Paul Boddie

        #4
        Re: AttributeError: 'tuple' object has no attribute 'encode'

        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

        Comment

        • erikcw

          #5
          Re: AttributeError: 'tuple' object has no attribute 'encode'

          On Apr 5, 12:37 pm, "Paul Boddie" <p...@boddie.or g.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.walk Data()
          File "/home/erik/Desktop/wa.py", line 91, in walkData
          sql = """INSERT INTO ag ('cid', 'ag', 'test') VALUES(%i, %s, %d)""",
          (cid.encode("ut f-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("ut f-8"), ag, self.data[parent][child]['results']['test'])
          self.cursor.exe cute(sql)

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

          Comment

          • Lenard Lindstrom

            #6
            Re: AttributeError: 'tuple' object has no attribute 'encode'

            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.


            Comment

            • Paul Boddie

              #7
              Re: AttributeError: 'tuple' object has no attribute 'encode'

              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

              Comment

              • Paul Boddie

                #8
                Re: AttributeError: 'tuple' object has no attribute 'encode'

                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.walk Data()
                File "/home/erik/Desktop/wa.py", line 91, in walkData
                sql = """INSERT INTO ag ('cid', 'ag', 'test') VALUES(%i, %s, %d)""",
                (cid.encode("ut f-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("ut f-8"), ag, self.data[parent][child]['results']['test'])
                self.cursor.exe cute(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.exe cute(sql) to self.cursor.exe cute(*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

                Comment

                Working...