how to display all the tables of my DB

375 views
Skip to first unread message

Mando

unread,
Mar 21, 2007, 6:44:58 AM3/21/07
to sqlalchemy
I need to display into a checkListBox (I'm writin a GUI with wxPython)
all the tables embedded in my Database.

Initially I thought that I must to do something like this:
>>>from sqlalchemy import *
>>>db = create_engine('sqlite:///tutorial.db')
>>>metadata = BoundMetaData(db)
>>>print metatada.tables
{}
>>>

but I've received only a empty dict.
What's the right method?

thanks

Paul Johnston

unread,
Mar 21, 2007, 7:52:07 AM3/21/07
to sqlal...@googlegroups.com
Hi,

The metadata only knows about the tables defined in SA. To get all the
tables in the db, do a query like:

select([information_schema.tables.c.table_name,
information_schema.tables.c.table_schema])

Paul

Mando

unread,
Mar 21, 2007, 4:59:05 PM3/21/07
to sqlalchemy
> select([information_schema.tables.c.table_name,
> information_schema.tables.c.table_schema])

Sorry, but I don't undestand how.

I saw that information_schema is a module inside sqlalchemy/databases/
information_schema.py, but I don't know how call it.

I've tried to do something like this:
>>>from sqlalchemy import *
>>select([information_schema.tables.c.table_name, information_schema.tables.c.table_schema])

Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>

select([information_schema.tables.c.table_name,information_schema.tables.c.table_schema])
NameError: name 'information_schema' is not defined

What's the right sintax or the part of the documentation I must to
read?

thank you very much

Paul Johnston

unread,
Mar 21, 2007, 5:10:20 PM3/21/07
to sqlal...@googlegroups.com
An example of a working script that uses this technique is here:
http://www.sqlalchemy.org/trac/attachment/wiki/UsageRecipes/AutoCode/autocode.py

Paul

Mando

unread,
Mar 22, 2007, 3:59:55 PM3/22/07
to sqlalchemy
I launched it, but I receive this error message:

Traceback (most recent call last):

File "autocode.py", line 20, in <module>
tbl = Table(tname, metadata, schema=schema, autoload=True);
File "build/bdist.macosx-10.3-fat/egg/sqlalchemy/schema.py", line
143, in __call__
File "build/bdist.macosx-10.3-fat/egg/sqlalchemy/engine/base.py",
line 505, in reflecttable
File "build/bdist.macosx-10.3-fat/egg/sqlalchemy/databases/
postgres.py", line 385, in reflecttable
KeyError: 'information_schema.cardinal_number'

Somes ideas?

thanks again!

vkuznet

unread,
Mar 23, 2007, 9:50:13 AM3/23/07
to sqlalchemy
Hi,
I've used slightly different approach:

sel="""SELECT table_name FROM all_tables WHERE owner='XXX'""" #
ORACLE
sel="show tables" # MySQL
sel="SELECT name FROM SQLITE_MASTER WHERE type='table'" # SQLite

con=dbengine.connect()
metadata=DynamicMetaData()
tList = con.execute(sel)
tables=[]
for t in tList:
tables.append(Table(t[0], metadata, autoload=True))

that will give you a list of auto-loaded Table objects.
Valentin.

Reply all
Reply to author
Forward
0 new messages