1

这个方法应该更新一个表,但我在最后一行得到一个操作 sql 错误:

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

堆栈跟踪:

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

我在插入元组时做错了,但我不知道是什么!

非常感谢!

4

1 回答 1

2

您不能将 SQL 参数用于表名或列名,只能用于列和结果值。

您必须使用传统的 python 字符串格式来插入更新表的名称。

于 2012-11-07T10:34:56.823 回答