How to get database metadata information (i.e. existing tables and columns in tables)

Discussion in 'Python' started by Chris Brat, Aug 22, 2006.

  1. Chris Brat

    Chris Brat Guest

    Hi,

    Is it possible to retrieve details about the database, specifically a
    list of the tables in the database; and then to retrieve the columns
    and their types for the tables?

    Is this dependant on the database?

    Thanks
    Chris
    Chris Brat, Aug 22, 2006
    #1
    1. Advertising

  2. Chris Brat wrote:

    > Is it possible to retrieve details about the database, specifically a
    > list of the tables in the database; and then to retrieve the columns
    > and their types for the tables?


    Yes.

    > Is this dependant on the database?


    Yes.

    Real databases usually store meta-data (list of tables, list of columns,
    list of indexes aso...) in system tables which can be queried in the same
    manner as common tables or view. Just read your database handbook...

    HTH
    Mathias
    Mathias Waack, Aug 22, 2006
    #2
    1. Advertising

  3. Chris Brat

    Miki Guest

    Hello Chris,

    > Is it possible to retrieve details about the database, specifically a
    > list of the tables in the database; and then to retrieve the columns
    > and their types for the tables?
    >
    > Is this dependant on the database?

    Yes and Yes. However some toolkits like SQLObject
    (http://www.sqlobject.org/) and SQLAlchemy (http://www.sqlalchemy.org/)
    can do this work for you (IIRC).

    HTH,
    Miki
    http://pythonwise.blogspot.com/
    Miki, Aug 22, 2006
    #3
  4. Chris Brat

    Chris Brat Guest

    Thanks for the great feedback.

    Chris.


    Chris Brat wrote:
    > Hi,
    >
    > Is it possible to retrieve details about the database, specifically a
    > list of the tables in the database; and then to retrieve the columns
    > and their types for the tables?
    >
    > Is this dependant on the database?
    >
    > Thanks
    > Chris
    Chris Brat, Aug 22, 2006
    #4
  5. Chris Brat

    Steve Holden Guest

    Re: How to get database metadata information (i.e. existing tablesand columns in tables)

    Chris Brat wrote:
    > Hi,
    >
    > Is it possible to retrieve details about the database, specifically a
    > list of the tables in the database; and then to retrieve the columns
    > and their types for the tables?
    >
    > Is this dependant on the database?
    >

    As far as locating the field names goes, this should work with most
    DBAPI modules:

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81189

    thought hat doesn't help wiht getting the table names ...

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
    Steve Holden, Aug 22, 2006
    #5
  6. If you want to know the names of the fields on a recordset, you can use
    cursor.description.
    For example, lets say you have a connection to a MySQL database:

    con = MySQLdb.connect('localhost','root','','mydb')
    cur = con.cursor()
    cur.execute('select * from customers')
    result = cur.fetchall()

    fields = [i[0] for i in cur.description]
    ....
    Description gives a list with information about your recordset, being
    the first item the name of the field.

    Hope this helps...
    Luis



    Chris Brat wrote:
    > Hi,
    >
    > Is it possible to retrieve details about the database, specifically a
    > list of the tables in the database; and then to retrieve the columns
    > and their types for the tables?
    >
    > Is this dependant on the database?
    >
    > Thanks
    > Chris
    =?iso-8859-1?q?Luis_M._Gonz=E1lez?=, Aug 22, 2006
    #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. RSH
    Replies:
    1
    Views:
    408
    Eliyahu Goldin
    Jun 2, 2005
  2. Replies:
    5
    Views:
    570
  3. Brett Selleck

    Schema Metadata not a Metadata Schema

    Brett Selleck, Sep 4, 2003, in forum: XML
    Replies:
    1
    Views:
    395
    Andy Dingley
    Sep 4, 2003
  4. Tim Golden
    Replies:
    0
    Views:
    415
    Tim Golden
    Aug 22, 2006
  5. Karthik
    Replies:
    1
    Views:
    300
    Thomas Tutone
    Oct 28, 2006
Loading...

Share This Page