[DB-SIG] Hoe to.

Tom Bryan tbryan@server.python.net
Wed, 29 Dec 1999 20:50:01 -0500 (EST)

On Tue, 28 Dec 1999, Wayne wrote:

> postgres database. The python code looks like this-
> def doReport(self, event = 0):
>       fileobj = open("testoutput", 'w')
>       db = _pg.connect('dbname', 'localhost')
>       myrecord = db.query('SELECT * FROM "tablename"')
>       fileobj.write(myrecord)

First of all, importing _pg directly is only necessary during initial
testing.  After that, you should be importing the Python program that
wraps _pg, probably something named pg or pgdb.  In this case, it probably
isn't the problem, but it's a good idea in any case.

> The error I'm getting is telling me that myrecord is a read only buffer.

When asking for help, you should really just post the traceback so that we
can see the entire error message.  Also, tell us what you've already
tried.  For example, does SELECT * from "tablename" work from PostgreSQL's
normal client?  Are you able to call 'print myrecord'?

In your case, the problem is unrelated to databases.  The file object's
write() method doesn't automaticaly invoke the __str__ method of an

Python 1.5.2 (#1, Apr 18 1999, 16:03:16)  [GCC pgcc-2.91.60 19981201
(egcs-1.1.1  on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> fh = open('junk','w')
>>> data = 3.14
>>> print data
>>> str(data)
>>> fh.write(data)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
TypeError: read-only buffer, float
>>> fh.write(str(data))
>>> fh.write('\n   %s' % data)
>>> fh.write('\n   %f\n' % data)
>>> fh.close()

$ cat junk

That is, make sure that you're passing a file object's write method a
string by explicitly converting the argument to a string.