将迭代器值传递给 Sqlite3,例如 python 中 select 子句的条件

Passing iterator value to Sqlite3 like condition of select clause in python

我有如下代码

inPath = 'D:\maddy'
outPath = 'D:\maddy\corpora'
fileName = 'genrelist.txt'
with sqlite3.connect('database.db') as connection,open(os.path.join(inPath, fileName), "r") as inputFile:
    myFileReader = csv.reader(inputFile)
    c = connection.cursor()
    for genre in myFileReader:
        '''store output in files named after each genre'''
        with open(os.path.join(outPath,'%s.txt' %genre[0]), "w") as outputFile:
            my_file_writer = csv.writer(outputFile,delimiter=",",quotechar="'")  
            c.execute("SELECT Plot FROM Movies WHERE Genre LIKE '%genre%' " )
            my_file_writer.writerows(c.fetchall())

这里的流派列表包含各种流派的名称,例如

动作 冒险 犯罪 戏剧 . . . .

当我尝试使用读取文件的流派遍历循环时,我无法将此值传递给 SELECT 子句中的 LIKE 条件,因为它将其视为字符串而不是具有动态值的变量。

请您帮我解决这个问题。

谢谢, 马迪

传递变量给sql的方法是:

 c.execute("SELECT Plot FROM Movies WHERE Genre LIKE ? ",('%'+genre+'%',) )