Re: Question(s) about the use of PgSQL.PgUnQuoteBytea

"Billy G. Allie" <[email protected]> Thu, 03 Jul 2003 22:08:06 -0400
Newsgroups gmane.comp.python.db.pypgsql.user
Organization michigan!/usr/group
Message-ID <[email protected]>
Luc Stepniewski wrote:

>I put some data in a table, which has a record of type 'bytea'.
>I inserted that data using the PgSQL.PgQuoteBytea method.
>
>When I want to extract the data, (I guess) I need, just after retrieving the 
>result(s) from a SELECT, to use PgSQL.PgUnQuoteBytea.
>But when I use it, I get the error "PgUnQuoteString() argument 1 must be 
>string, not instance' ".
>Doing a "print myinstance.__class__"  prints 'pyPgSQL.PgSQL.PgBytea'. So I 
>guess it's ok, so why do I get an error?
>Here is the snippet of code I used:
>
>===========
>    recup = """SELECT docbody FROM docs where docid=%s """ %
>                    PgSQL.PgQuoteString(form.getvalue('docid'))
>    cur.execute(recup)
>    res = cur.fetchall()
>    print PgSQL.PgUnQuoteBytea(res[0]['docbody'])
>===========
>
>Looking at the documentation for the methods, I see that PgUnQuoteBytea takes 
>a string as parameter (!?), so if I try to stringify it, I get the following 
>error: "Bad input string for type bytea", doing this:
>===========
>c = `res[0]['docbody']`
>d = PgSQL.PgUnQuoteBytea(c)
>===========
>
>
>Any idea?
>  
>
You are trying to do thingx which pyPgSQL is already doing for you.  To 
print a  field that is defined in the database as bytea, just print it.  
It's already been processed by PyUnQuoteBytea.  In your example:

    rcup = "SELECT docbody FROM docs WHERE docid = %s"
    cur.execute(recup, form.getvalue('docikk'))
    res - cur.fetchall()
    print res[0]['docbody']

You should not have to use PgQuotString() or PgUnQuoteBytea() directly.  
In order to place a bytea value into the database, you can create a new 
PgBytea object and passs that object to execute as a paramter:

    cur.execute("""INSERT INTO docs (docid, docbody)
                    VALUES(%s, %s)""", docid, PgBytea(docbody))

I hope this clarifies things for you.



-- 
___________________________________________________________________________
____       | Billy G. Allie    | Domain....: [email protected]
|  /|      | 7436 Hartwell     | MSN.......: [email protected]
|-/-|----- | Dearborn, MI 48126|
|/  |LLIE  | (313) 582-1540    |