0

假设我有一个名为my_list

test仅当data列中的值存在时,我才想从表中获取行my_list

一个假设的代码就像

import sqlite3 as lite

conn = lite.connect ('database.db')
with conn:
    conn.row_factory = lite.Row
    curr = conn.cursor ()
    curr.execute ('''select * from test
                     where data in :list''', {'list': my_list}) //I know this is not the correct syntax

那么,如何在Python中解决以下问题

4

1 回答 1

1

首先建立您的查询字符串,然后执行它传递您的列表作为参数...

query = 'select * from test where data in ({})'.format(','.join(['?']*len(my_list)))
curr.execute(query, my_list)
于 2013-05-28T10:05:32.357 回答