471,624 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,624 software developers and data experts.

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

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

Aug 22 '06 #1
5 3187
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

Aug 22 '06 #2
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/

Aug 22 '06 #3
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
Aug 22 '06 #4
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/Coo...n/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

Aug 22 '06 #5
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
Aug 22 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mario T. Lanza | last post: by
2 posts views Thread by sloan | last post: by
1 post views Thread by Karthik | last post: by
1 post views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.