Strings and % sign fails - Help Please

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • siasookhteh@gmail.com

    #1

    Strings and % sign fails - Help Please

    I also posted this in Django Users group, but figured it probably has
    more relevance for python group.

    It seems like a freak problem to me. I spent a long hour to track the
    problem down and here it is:

    The following statement fails because it has the '%' sign in it.
    cursor.execute( "select '%'")

    The error is: IndexError: list index out of range

    How do I address this problem?

    Please note that the following work just fine:
    cursor.execute( "select 'x'")

    and the following also fails with the same error:
    cursor.execute( """select '%'""")
    cursor.execute( "select '\%'")

    astr = "select '\%'"
    cursor.execute( astr)

    I greatly appreciate all helps,
    Regards,
    Sia

  • Jorge Godoy

    #2
    Re: Strings and % sign fails - Help Please

    siasookhteh@gma il.com writes:
    [color=blue]
    > The following statement fails because it has the '%' sign in it.
    > cursor.execute( "select '%'")
    >
    > The error is: IndexError: list index out of range
    >
    > How do I address this problem?[/color]

    Use "%%".


    --
    Jorge Godoy <godoy@ieee.org >

    "Quidquid latine dictum sit, altum sonatur."
    - Qualquer coisa dita em latim soa profundo.
    - Anything said in Latin sounds smart.

    Comment

    • siasookhteh@gmail.com

      #3
      Re: Strings and % sign fails - Help Please

      heh.. It works except I am using psycopg.Binary( somebinarystruc ture),
      and I am not really doing it by hand to just add the extra %, and
      psycopg.Binary doesn't do it. I'd imagine it's a bug with psycopg
      package. Any quick way to project a string from freak '%' problems?

      Thanks,
      Sia


      Jorge Godoy wrote:[color=blue]
      > siasookhteh@gma il.com writes:
      >[color=green]
      > > The following statement fails because it has the '%' sign in it.
      > > cursor.execute( "select '%'")
      > >
      > > The error is: IndexError: list index out of range
      > >
      > > How do I address this problem?[/color]
      >
      > Use "%%".
      >
      >
      > --
      > Jorge Godoy <godoy@ieee.org >
      >
      > "Quidquid latine dictum sit, altum sonatur."
      > - Qualquer coisa dita em latim soa profundo.
      > - Anything said in Latin sounds smart.[/color]

      Comment

      • Jorge Godoy

        #4
        Re: Strings and % sign fails - Help Please

        siasookhteh@gma il.com writes:
        [color=blue]
        > heh.. It works except I am using psycopg.Binary( somebinarystruc ture),
        > and I am not really doing it by hand to just add the extra %, and
        > psycopg.Binary doesn't do it. I'd imagine it's a bug with psycopg
        > package. Any quick way to project a string from freak '%' problems?[/color]

        Try using r"string '%'"...

        --
        Jorge Godoy <godoy@ieee.org >

        "Quidquid latine dictum sit, altum sonatur."
        - Qualquer coisa dita em latim soa profundo.
        - Anything said in Latin sounds smart.

        Comment

        • Erik Max Francis

          #5
          Re: Strings and % sign fails - Help Please

          Jorge Godoy wrote:
          [color=blue]
          > siasookhteh@gma il.com writes:
          >[color=green]
          >> heh.. It works except I am using psycopg.Binary( somebinarystruc ture),
          >> and I am not really doing it by hand to just add the extra %, and
          >> psycopg.Binary doesn't do it. I'd imagine it's a bug with psycopg
          >> package. Any quick way to project a string from freak '%' problems?[/color]
          >
          > Try using r"string '%'"...[/color]

          Raw strings don't have anything to do with format specifiers.

          --
          Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
          San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
          If the sky should fall, hold up your hands.
          -- (a Spanish proverb)

          Comment

          • Jorge Godoy

            #6
            Re: Strings and % sign fails - Help Please

            Erik Max Francis <max@alcyone.co m> writes:
            [color=blue]
            > Raw strings don't have anything to do with format specifiers.[/color]

            I know. I'm just trying to see if there might be some magic going on with his
            driver...

            --
            Jorge Godoy <godoy@ieee.org >

            "Quidquid latine dictum sit, altum sonatur."
            - Qualquer coisa dita em latim soa profundo.
            - Anything said in Latin sounds smart.

            Comment

            • Erik Max Francis

              #7
              Re: Strings and % sign fails - Help Please

              Jorge Godoy wrote:
              [color=blue]
              > I know. I'm just trying to see if there might be some magic going on with his
              > driver...[/color]

              Since raw strings have no effect on format specifiers, that won't tell
              you anything.
              [color=blue][color=green][color=darkred]
              >>> r'%' == '%'[/color][/color][/color]
              True

              His problem is that cursor.execute does format expansion with %, so a
              single % is not legal.

              --
              Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
              San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
              We are victims of our circumstance.
              -- Sade Adu

              Comment

              • Christoph Zwerschke

                #8
                Re: Strings and % sign fails - Help Please

                Erik Max Francis wrote:[color=blue]
                > His problem is that cursor.execute does format expansion with %, so a
                > single % is not legal.[/color]

                Yes, I think psycopg uses paramstyle='pyf ormat', i.e. it expands
                parameters in your sql in the usual Python way where % has a special
                meaning. If you really mean the % sign only, use %%.

                -- Christoph

                Comment

                • Fredrik Lundh

                  #9
                  Re: Strings and % sign fails - Help Please

                  siasookhteh@gma il.com wrote:
                  [color=blue]
                  > heh.. It works except I am using psycopg.Binary( somebinarystruc ture),
                  > and I am not really doing it by hand to just add the extra %, and
                  > psycopg.Binary doesn't do it. I'd imagine it's a bug with psycopg
                  > package.[/color]

                  or a bug in your use of Binary. what exactly are you applying Binary
                  to, and how do you pass the resulting adapter to execute ?

                  </F>



                  Comment

                  • Siah

                    #10
                    Re: Strings and % sign fails - Help Please

                    Problem Solved. The problem was with psycopg.Binary whose mere job is
                    to convert bytes into clear text for sql statement. I'll be filing a
                    bug.

                    Thanks everyone,
                    Sia

                    Comment

                    • Fredrik Lundh

                      #11
                      Re: Strings and % sign fails - Help Please

                      Siah wrote:
                      [color=blue]
                      > Problem Solved. The problem was with psycopg.Binary whose mere job is
                      > to convert bytes into clear text for sql statement.[/color]

                      no, its job is to wrap strings that contain binary blobs, so that data binding
                      works properly. you're supposed to do

                      cursor.execute( statement, Binary(data))

                      and not

                      cursor.execute( Binary(statemen t))

                      or

                      cursor.execute( "some statement %s" % Binary(statemen t))

                      or some other sillyness. it's an object wrapper, not an escape function for
                      arbitrary SQL. Python's not PHP.

                      for more details, see "Type Objects and Constructors" on this page

                      This API has been defined to encourage similarity between the Python modules that are used to access databases. By doing this, we hope to achieve a consistency leading to more easily understood modules, code that is generally more portable across datab...


                      </F>



                      Comment

                      Working...