[Tutor] pgdb and console output

pylist1 pylist1 at gmail.com
Tue Mar 9 15:06:00 CET 2010


On Mon, 8 Mar 2010, Alan Gauld wrote:

> "hithere there" <pylist1 at gmail.com> wrote
>>  #! /usr/bin/env python
>>
>>  import pgdb, sys
>>
>>  db = pgdb.connect (dsn='192.168.0.1:familydata',
>>     user='postgres', password='')
>>  cursor = db.cursor ()
>>
>>  cursor.execute ("select * from names")
>>
>>  rows = cursor.fetchall ()
>>
>>  for i in (rows):
>>     print i
>>
>>  #viewtable (db)
>
> No idea what viewtable does, never seen it before...
> The only case I found on Google was in a tutorial which defined viewtable() 
> as a function, it wasn't in the pgdb module...
>
>>  #sys.stdout.write()
>
> This writes nothing to stdout. Apparently successfully from your comment 
> below.
>
> Try:
>
> sys.stdout.write(str(i))

Yes that does indeed work like this:

for i in (rows):
     sys.stdout.write(str(i))

But it leaves formatting issues with leaving the command prompt in the 
middle of the screen. That's not an issue at the moment.

>>  The code as is will display the data to the console.  I have read the
>>  db API 2.0 at python.org.  The problem is viewtable (db) will not work
>>  or sys.stdout.write () to write the table data to the console screen.
>>  What am I doing wrong here or what do I need to do different?
>
> The print is writing to the console I assume? That is the normal method.
>
>>  Can anyone point me to a book specificly for pgdb, python and postgre?
>
> It doesn't look lie you need a  postgres specific boook but just to go 
> through the standard Python tutorial. It should make most things clear.
>
>>  Last thing is can I define the classes in a separate files and
>>  reference them?  Kinda like code reuse in .Net?  I new to Python so
>>  bear with me.
>
> Yes, just define them in a file then import that file as a module. The 
> standard tutorial explains that too.

Ok got that I think. Python references the current running directory first 
then to site packages directory.

I found after my initial email your pdf book you had wrote.  it seems to 
have exactly what I am after.  The console sqlite addressbook.py 
application.  That is the way I am wantting to do mine.  Going to use the 
way you done but with Postgre and pgdb and see how I do.  Thanks for 
writting the book.  It was a big help atleast that part.

One last thing is your using "raw_input".  What's the differenct between 
that and "str input"?  If the OS is using UTF-8 and the databse is UTF-8 
should it really matter? So raw_input could mean any type of input?

thankyou


More information about the Tutor mailing list