Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中尝试使用cursor.executemany(字符串,元组)时引发SQLite操作错误_Python_Sqlite_Tuples - Fatal编程技术网

在Python中尝试使用cursor.executemany(字符串,元组)时引发SQLite操作错误

在Python中尝试使用cursor.executemany(字符串,元组)时引发SQLite操作错误,python,sqlite,tuples,Python,Sqlite,Tuples,此方法应更新表,但我在最后一行得到一个操作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 rand

此方法应更新表,但我在最后一行得到一个操作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
回溯(最近一次呼叫最后一次):
文件“C:\Users\jgoddijn\workspace\DataAnonDMVV\src\Main.py”,第119行,在
更新查找(“AIS”、“MMSI\U查找”、“AIS\U异常”、“dad\u vesselname”、“AIS\u schip”)
文件“C:\Users\jgoddijn\workspace\DataAnonDMVV\src\Main.py”,第35行,在更新查找中
c、 ExecuteMy('UPDATE?SET?=?WHERE?=?',lst_rev)
sqlite3.0错误:靠近“”:语法错误
我的元组插入有问题,但我不知道是什么


非常感谢

不能对表或列名使用SQL参数,只能对列和结果值使用SQL参数


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

只是想知道-您是否尝试直接插入表名-例如:c.executemany('UPDATE'+annon_table+'SET?=?WHERE?=?',lst_rev)和/或在调用前打印输出lst_rev?