Selection error...

Discussion in 'Python' started by Abandoned, Oct 21, 2007.

  1. Abandoned

    Abandoned Guest

    Hi..
    I want to select datas but some datas give me this error:

    psycopg2.ProgrammingError: invalid byte sequence for encoding "UTF8":
    0xc720
    HINT: This error can also happen if the byte sequence does not match
    the encoding expected by the server, which is controlled by
    "client_encoding".

    My select command:
    cursor.execute("SELECT id from templinks where url='%s'"% URL)

    This problem occured only select data when i try insert the same data
    it doesn't give me error..
    Note: My database is UTF-8 and the URLS come from different web sites'
    html code
    How can i fix this problem ?
    I'm sorry my bad english..
     
    Abandoned, Oct 21, 2007
    #1
    1. Advertising

  2. On Sat, 2007-10-20 at 18:28 -0700, Abandoned wrote:
    > Hi..
    > I want to select datas but some datas give me this error:
    >
    > psycopg2.ProgrammingError: invalid byte sequence for encoding "UTF8":
    > 0xc720
    > HINT: This error can also happen if the byte sequence does not match
    > the encoding expected by the server, which is controlled by
    > "client_encoding".
    >
    > My select command:
    > cursor.execute("SELECT id from templinks where url='%s'"% URL)


    Haven't we told you before not to use the % operator to fill values into
    a query? Use parameter binding:

    cursor.execute("SELECT id from templinks where url=%s", (URL,) )

    > This problem occured only select data when i try insert the same data
    > it doesn't give me error..
    > Note: My database is UTF-8 and the URLS come from different web sites'
    > html code


    Your string probably contains latin-1 encoded text. Try
    URL.decode('latin-1').encode('utf-8') to transcode it into utf-8.

    Hope this helps,

    --
    Carsten Haese
    http://informixdb.sourceforge.net
     
    Carsten Haese, Oct 21, 2007
    #2
    1. Advertising

  3. On Sat, 20 Oct 2007 22:47:23 -0400, Carsten Haese <>
    declaimed the following in comp.lang.python:


    > Haven't we told you before not to use the % operator to fill values into
    > a query? Use parameter binding:
    >
    > cursor.execute("SELECT id from templinks where url=%s", (URL,) )
    >

    And check the adapter documentation with regards to the placeholder
    it uses... It may just want a ? instead of %s
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Oct 21, 2007
    #3
  4. On Sat, 20 Oct 2007 21:06:31 -0700, Dennis Lee Bieber wrote
    > On Sat, 20 Oct 2007 22:47:23 -0400, Carsten Haese <>
    > declaimed the following in comp.lang.python:
    >
    > > Haven't we told you before not to use the % operator to fill values into
    > > a query? Use parameter binding:
    > >
    > > cursor.execute("SELECT id from templinks where url=%s", (URL,) )
    > >

    > And check the adapter documentation with regards to the placeholder
    > it uses... It may just want a ? instead of %s


    The OP uses psycopg2, which unfortunately uses %s parameter notation. But I
    agree, it's worth noting that the parameter style may vary between different
    DB-API implementations (until the next version of DB-API, when supporting at
    least qmark and named parameter styles becomes mandatory, yay!).

    --
    Carsten Haese
    http://informixdb.sourceforge.net
     
    Carsten Haese, Oct 21, 2007
    #4
  5. Abandoned

    Abandoned Guest


    > cursor.execute("SELECT id from templinks where url=%s", (URL,) )

    Yes i already try this before ask.

    URL.decode('latin-1').encode('utf-8') to transcode it into utf-8.
    i know this but how do you know the html encoding's a latin-1 ? Html
    encoding is to be iso-8859-9 or ascii or cp1254 ... For this reason
    URL.decode('latin-1').encode('utf-8') must give me error. I'm trying
    now and whet it finished i write the result here.
    Thank you very much for your helps.
     
    Abandoned, Oct 21, 2007
    #5
  6. 2007/10/21, Abandoned <>:
    >
    > > cursor.execute("SELECT id from templinks where url=%s", (URL,) )

    > Yes i already try this before ask.
    >
    > URL.decode('latin-1').encode('utf-8') to transcode it into utf-8.
    > i know this but how do you know the html encoding's a latin-1 ?


    Try using chardet to detect the encoding

    > Html
    > encoding is to be iso-8859-9 or ascii or cp1254 ... For this reason
    > URL.decode('latin-1').encode('utf-8') must give me error. I'm trying
    > now and whet it finished i write the result here.
    > Thank you very much for your helps.
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >



    --
    -- Guilherme H. Polo Goncalves
     
    Guilherme Polo, Oct 21, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Niederberger
    Replies:
    2
    Views:
    16,527
    Christian Kaufhold
    Jan 7, 2005
  2. Andrew Crowe
    Replies:
    1
    Views:
    4,471
    Andrew Crowe
    Sep 13, 2004
  3. curiousity

    HOWTO autopost the selection list upon selection

    curiousity, Nov 21, 2003, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    271
    curiousity
    Nov 21, 2003
  4. Loebb
    Replies:
    0
    Views:
    203
    Loebb
    Feb 23, 2004
  5. Replies:
    1
    Views:
    211
Loading...

Share This Page