Use pyodbc to count and list tables, columns, indexes, etc

DFS nospam at dfs.com
Thu Mar 31 23:44:23 EDT 2016


================================================================
import pyodbc

dbName = "D:\test_data.mdb"
conn = pyodbc.connect('DRIVER={Microsoft Access Driver 
(*.mdb)};DBQ='+dbName)
cursor = conn.cursor()

#COUNT TABLES, LIST COLUMNS
tblCount = 0
for rows in cursor.tables():
	if rows.table_type == "TABLE":  #LOCAL TABLES ONLY
		tblCount += 1
		print rows.table_name
		for fld in cursor.columns(rows.table_name):
			print(fld.table_name, fld.column_name)
			
print tblCount,"tables"
================================================================

Problem is, the 'for rows' loop executes only once if the 'for fld' loop 
is in place.  So even if I have 50 tables, the output is like:

DATA_TYPES
(u'DATA_TYPES', u'FLD_TEXT', -9, u'VARCHAR')
(u'DATA_TYPES', u'FLD_MEMO', -10, u'LONGCHAR')
(u'DATA_TYPES', u'FLD_NBR_BYTE', -6, u'BYTE')
1 tables

And no errors are thrown.

If I comment out the 2 'for fld' lines, it counts and lists all 50 
tables correctly.

Any ideas?

Thanks!




More information about the Python-list mailing list