我有一个单词列表(超过100k个元素)。在数据库中,每个单词都有对应的id
。我也想将这些ID列入列表。
我正在使用以下功能,但是速度很慢:
def fetch_id(word_list, cursor):
for word in word_list:
cursor.execute('SELECT id FROM entries_table WHERE word = ?',(word,))
data = cursor.fetchone()
if data is not None:
return data[0]
else: return None
有没有一种使功能更快的方法?
答案 0 :(得分:0)
cursor.execute("SELECT id FROM entries_table WHERE word in {}".format(str(tuple(word_list))))