Extracting DB schema (newbie Q)

Chris Angelico rosuav at gmail.com
Mon May 14 12:11:06 EDT 2012


On Tue, May 15, 2012 at 2:01 AM, Steve Sawyer <ssawyer at stephensawyer.com> wrote:
> Brand-new to Python (that's a warning, folks)

It's one we're familiar with :) Welcome!

> Trying to write a routine to import a CSV file into a SQL Server
> table. To ensure that I convert the data from the CSV appropriately,
> I"m executing a query that gives me the schema (data column names,
> data types and sizes) from the target table.
>
> 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).

That seems reasonable; I might consider a namedtuple or perhaps
another dictionary, but what you have is usable.

> 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.

Not sure what you're attempting to do; you'd do well to post your code
(preferably a minimal test-case) and the exact traceback. But my guess
is that you're creating a list and then trying to use the update()
method. If that's so, you can simplify it a lot:

columninfo[columnname] = [type, length, precision]

> What I was able to make work was to construct the command as a string
> and run exec(<python command>), but seems there shoudl be a more
> direct way of updating the dictionary.

Agreed, you shouldn't normally need to exec to achieve what you want!
But post your failing code and we'll be better able to help.

Chris Angelico



More information about the Python-list mailing list