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

C

Chris Brat

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
 
M

Mathias Waack

Chris said:
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
 
?

=?iso-8859-1?q?Luis_M._Gonz=E1lez?=

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top