Reputation: 2232
this method should update a table, but i get a operational sql error at the last line:
def update_lookup(table, lookup_table, anon_table, fieldname, anon_field_prefix):
c.execute('SELECT DISTINCT ' + fieldname + ' FROM ' + table)
result = c.fetchall() #gives a list of tuples
#get a list of random integers in range, as many as there are unique mssi's
rnds = random.sample(xrange(10000,80000),len(result))
#for every name insert the value in the vesselname lookup
lst = []
lst_rev = []
for i,row in enumerate(result):
field_value_anon = anon_field_prefix + str(rnds[i]) #create a random mssi number
field_value = row[0]
lst_rev.append((anon_table, fieldname , field_value_anon, fieldname, field_value)) #needed later on
lst.append((field_value, field_value_anon))
c.executemany('INSERT INTO '+ lookup_table +' VALUES (null, ?, ?)', lst)
#update the anon table col:
c.executemany('UPDATE ? SET ?=? WHERE ?=?', lst_rev) //error is trhown
stacktrace:
Traceback (most recent call last):
File "C:\Users\jgoddijn\workspace\DataAnonDMVV\src\Main.py", line 119, in <module>
update_lookup('AIS', 'MMSI_lookup', 'AIS_anoniem', 'dad_vesselname', 'AIS_schip')
File "C:\Users\jgoddijn\workspace\DataAnonDMVV\src\Main.py", line 35, in update_lookup
c.executemany('UPDATE ? SET ?=? WHERE ?=?', lst_rev)
sqlite3.OperationalError: near "?": syntax error
I'm doing something wrong with the tuple insertion but i cannot figure what!
thanks a bunch!
Upvotes: 1
Views: 430
Reputation: 1122152
You cannot use SQL parameters for table or column names, only for column and result values.
You'll have to use conventional python string formatting to insert the name of the updated table instead.
Upvotes: 2