[Tutor] My Name is John and I copy and paste

John CORRY john.corry at ntlworld.com
Tue Apr 18 11:08:52 CEST 2006


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])))
    c.execute('UPDATE cost_grid SET cost_2 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[6]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_2 = ? where cost_grid_id = ? and
finish_dro = ?', ( float(row[7]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_2 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[8]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_3 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[9]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_3 = ? where cost_grid_id = ? and
finish_dro = ?', ( float(row[10]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_3 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[11]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_4 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[12]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_4 = ? where cost_grid_id = ? and
finish_dro = ?', ( float(row[13]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_4 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[14]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_5 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[15]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_5 = ? where cost_grid_id = ? and
finish_dro = ?', ( float(row[16]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_5 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[17]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_6 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[18]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_6 = ? where cost_grid_id = ? and
finish_dro = ?', ( float(row[19]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_6 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[20]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_7 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[21]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_7 = ? where cost_grid_id = ? and
finish_dro = ?', ( float(row[22]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_7 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[23]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_8 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[24]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_8 = ? where cost_grid_id = ? and
finish_dro = ?', ( float(row[25]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_8 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[26]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_9 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[27]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_9 = ? where cost_grid_id = ? and
finish_dro = ?', ( float(row[28]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_9 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[29]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_10 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[30]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_10 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[31]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_10 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[32]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_11 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[33]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_11 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[34]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_11 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[35]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_12 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[36]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_12 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[37]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_12 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[38]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_13 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[39]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_13 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[40]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_13 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[41]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_14 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[42]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_14 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[43]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_14 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[44]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_15 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[45]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_15 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[46]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_15 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[47]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_16 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[48]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_16 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[49]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_16 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[50]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_17 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[51]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_17 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[52]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_17 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[53]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_18 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[54]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_18 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[55]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_18 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[56]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_19 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[57]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_19 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[58]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_19 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[59]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET cost_20 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[60]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET rrp_20 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[61]), row[0], float(row[2])))
    c.execute('UPDATE cost_grid SET ie_rrp_20 = ? where cost_grid_id = ?
and finish_dro = ?', ( float(row[62]), row[0], float(row[2])))
    
    db.commit()
    c.close()    
 
Thanks,
 
John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060418/5391464b/attachment.htm 


More information about the Tutor mailing list