python - Updating SQL Table Where Fields May be Null -
i have sql table want update daily. data records need updated pulled web, text file. have python script read data, , figure out records need updated.
given potentially large number of records updated each day, following:
with get_connection_to('database') connection: cursor = connection.cursor() cursor.executemany(query, data)
the problem approach of fields may potenitall null
\ none
so if use query like
update tablename set active = 0 field1 = ? , field2 = ? , field3 = ? , field4 = ? , field5 = ?
then records contain null
value won't updated.
the best idea i've come far loop on each row, inspect each element, construct sql query each update, , perform each transaction separately. feels inelegant, , slow solution problem.
has got better solution?
update tablename set active = 0 nullif(?,field1) null , nullif(?,field2) null , nullif(?,field3) null , nullif(?,field4) null , nullif(?,field5) null
Comments
Post a Comment