Skip to content

Instantly share code, notes, and snippets.

@jbiason
Created September 13, 2020 13:51
Show Gist options
  • Save jbiason/47b6858c13d6e0cc03bb15df7e371f5d to your computer and use it in GitHub Desktop.
Save jbiason/47b6858c13d6e0cc03bb15df7e371f5d to your computer and use it in GitHub Desktop.
How to make "select in" with SQLite in Python
import sqlite3
import itertools
names = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty']
groups = itertools.cycle([1, 2, 3])
ids = itertools.count(1)
data = zip(ids, names, groups)
con = sqlite3.connect(':memory:')
cur = con.cursor()
cur.execute('''create table lite (id int, name text, grp int)''')
cur.executemany('INSERT INTO lite values (?,?,?)', data)
filters = (1,3)
query = 'select name, grp from lite where grp in ({})'.format(
', '.join('?' * len(filters)))
print(query)
for record in cur.execute(query, filters).fetchall():
print(record)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment