Thread: viewing the description of tables from python DB-API

viewing the description of tables from python DB-API

From
"Daniel Joo"
Date:
<div class="Section1"><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">Hi all,</span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">Is there a way to view the list of all tables from python (or any other languages for that matter)
DB-API? What I’m looking for is a command similar to the meta-command ‘\d’ that works with the psql client. 
</span></font><pclass="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">Thanks very much!</span></font><p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt;
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">Dan</span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial"> </span></font></div>

Re: viewing the description of tables from python DB-API

From
Alvaro Herrera
Date:
Daniel Joo wrote:

> Is there a way to view the list of all tables from python (or any other
> languages for that matter) DB-API?  What I'm looking for is a command
> similar to the meta-command '\d' that works with the psql client.  

Try \d under psql -E sometime.  It'll give you the query it uses.

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


Re: viewing the description of tables from python DB-API

From
"Rodrigo De Leon"
Date:
On 8/1/06, Daniel Joo <djoo@vistagen-inc.com> wrote:
> Hi all,
>
>
>
> Is there a way to view the list of all tables from python (or any other
> languages for that matter) DB-API?  What I'm looking for is a command
> similar to the meta-command '\d' that works with the psql client.
>
>
>
> Thanks very much!
>
>
>
> Dan

There's also the INFORMATION_SCHEMA:

http://www.postgresql.org/docs/8.1/static/information-schema.html

Regards,

Rodrigo


Re: viewing the description of tables from python DB-API

From
Scott Marlowe
Date:
On Tue, 2006-08-01 at 18:14, Rodrigo De Leon wrote:
> On 8/1/06, Daniel Joo <djoo@vistagen-inc.com> wrote:
> > Hi all,
> >
> >
> >
> > Is there a way to view the list of all tables from python (or any other
> > languages for that matter) DB-API?  What I'm looking for is a command
> > similar to the meta-command '\d' that works with the psql client.
> >
> >
> >
> > Thanks very much!
> >
> >
> >
> > Dan
> 
> There's also the INFORMATION_SCHEMA:
> 
> http://www.postgresql.org/docs/8.1/static/information-schema.html

Quick note.  The INFORMATION_SCHEMA should stay pretty static from one
pgsql version to the next, while the queries that psql -E shows will
likely change from one version to another.


Re: viewing the description of tables from python DB-API

From
George Young
Date:
On Tue, 1 Aug 2006 15:30:48 -0700
"Daniel Joo" <djoo@vistagen-inc.com> wrote:

> Is there a way to view the list of all tables from python (or any other
> languages for that matter) DB-API?  What I'm looking for is a command
> similar to the meta-command '\d' that works with the psql client.  

The cursor object has a "description" data member.  
Look at the DB-API spec:  http://www.python.org/dev/peps/pep-0249/
under "Cursor Objects".

Python 2.3.3 (#1, Jan  3 2004, 07:17:11) 
[GCC 3.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pgdb
>>> db=pgdb.connect(database='pigrestore', host='ivy')
>>> cur=db.cursor()
>>> cur.execute('select * from machine')
>>> print cur.description
[('machine_name', 'text', -1, -1, None, None, None), ('machine_id', 'int2', -1, 2, None, None, None), ('area', 'text',
-1,-1, None, None, None), ('text', 'text', -1, -1, None, None, None)]
 
>>> 

-- 
"Are the gods not just?"  "Oh no, child.
What would become of us if they were?" (C.S. Lewis)