Created
September 13, 2020 13:51
-
-
Save jbiason/47b6858c13d6e0cc03bb15df7e371f5d to your computer and use it in GitHub Desktop.
How to make "select in" with SQLite in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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