[Tutor] My Name is John and I copy and paste
Bob Gailer
bgailer at alum.rpi.edu
Tue Apr 18 17:19:08 CEST 2006
John CORRY wrote:
>
> Hi,
>
>
>
> Thanks for all the help so far on my database questions. I have now
> developed a program that reads in info from a csv file and updates
> already existing records in my database. I have enclosed the code
> below. The good news is that it is working. However, I used copy and
> paste and amended the text to write the code and it seems rather
> longwinded. Is there a quicker/better way to write the code below?
>
>
>
> path = "c:/test/import.csv"
>
> import mx.ODBC
>
> import mx.ODBC.Windows
>
> import csv
>
> reader = csv.reader(open(path,"rb"))
>
> for row in reader:
>
> db = mx.ODBC.Windows.DriverConnect('DSN=vfp')
>
> c = db.cursor()
>
> c.execute('UPDATE cost_grid SET cost_1 = ? where cost_grid_id = ?
> and finish_dro = ?', ( float(row[3]), row[0], float(row[2])))
>
> c.execute('UPDATE cost_grid SET rrp_1 = ? where cost_grid_id = ?
> and finish_dro = ?', ( float(row[4]), row[0], float(row[2])))
>
> c.execute('UPDATE cost_grid SET ie_rrp_1 = ? where cost_grid_id =
> ? and finish_dro = ?', ( float(row[5]), row[0], float(row[2])))
>
When I see "repeated columns" I think the database is not fully
normalized. If this were my database I'd have a table with columns named
cost, rrp, ie_rrp, cost_grid_id, finish_dro, and a column to distingush
the 20 cases. A little more work up front, but a lot easier to work with
once it is set up.
The recommendation to do this in a loop is the answer regardless of the
data model.
[snip]
More information about the Tutor
mailing list