Extracting DB schema (newbie Q)

Steve Sawyer ssawyer at stephensawyer.com
Mon May 14 15:09:50 EDT 2012


Thanks, John.

>What are you trying that isn't working?
Typical newbie trick - trying to make things more complicated than
they are. I didn't realize that syntax would establish the key/value
pairs of the dictionary - I thought that would only allow you to
establish the value to correspond to a specified (and pre-exiting)
key, not establish the key as well.

I was establishing the dictionary then trying to use dict.update() to
append key/value pairs.

Thanks - now, given my query that returns the table structure, this
works fine:

table_dict = {}
table_specs = cursor.execute(query_string)
for row in table_specs:
	row_dict = {}
	row_dict['type'] = row.DataType
	row_dict['size'] = row.Length
	table_dict[row.name] = row_dict

table_dict['path']['type'] #-> 'nvarchar'
table_dict['path']['size'] # -> 200
table_dict['Artist']['size'] #-> 50

Is this (nesting dictionaries) a good way to store multiple attributes
associated with a single key value?


On Mon, 14 May 2012 17:05:17 +0000 (UTC), John Gordon
<gordon at panix.com> wrote:

>In <bca2r7dt23l49ovp7m7id1ap3iaccvk7pe at 4ax.com> Steve Sawyer <ssawyer at stephensawyer.com> writes:
>
>> What I think I want to do is to construct a dictionary using the
>> column names as the index value, and a list containing the various
>> attributes (data type, lenghth, precision).
>
>If you're using just the column name as the dictionary key, make sure
>there are no duplicate column names among all your tables.
>
>> If this is a good approach, I ran into a problem populating the
>> dictionary as I couldn't seem to figure out how to make the update()
>> method work by passing the name property of the row object; I kept
>> getting a "keyword can't be an expression" error.
>
>The general syntax for assigning to a dictionary is:
>
>  my_dictionary[key] = value
>
>What are you trying that isn't working?
--Steve--



More information about the Python-list mailing list