I am looking for an sqlite3 command that let’s me select entries given by a tuple, let me explain with an example:
Here is my data:
my_data = [(1,8),(2,4),(3,5),(4,7),(5,13)]
and I am trying to extract entries who’s first values are either 1,2 or 4; hence, my desired output is:
[(1, 8), (2, 4), (4, 7)]
I can achieve that with a code below; however, I think that my code is not optimal:
import sqlite3
my_data = [(1,8),(2,4),(3,5),(4,7),(5,13)]
key_indexes = (1,2,4)
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute('''CREATE TABLE my_table
(val_1 INTEGER, val_2 INTEGER)''')
for entry in my_data:
c.execute('''INSERT INTO my_table VALUES(?,?)''',entry)
conn.commit()
result = []
for ind in key_indexes:
c.execute('''SELECT* FROM my_table WHERE val_1 = ?''', (ind,))
temp_res = c.fetchall()
result.extend(temp_res)
I am looking for a code that can replace the for loop at the and with an sqlite3 command.
I want to stick (1,2,4) somewhere in this line:
c.execute('''SELECT* FROM my_table WHERE val_1 = ?''', (ind,))
instead of doing a for loop.
Thank You in Advance
To replace the last for-loop you can build up the list of indexes/indices in a string and hit the database just once.
Please note that this is a two-step process, and not vulnerable — in and of itself — to SQL injection attacks.
Additionally:
You didn’t directly ask about this, but the first
forloop and call toexecute()could be reduced to a single call toexecutemany().You should test which of the two options is faster because the DB-API doesn’t specify exactly how
executemany()should be implemented; performance may differ across RDBMSs.You may read up on
executemany()here: http://www.python.org/dev/peps/pep-0249/For now, suffice it to say that it takes as the second argument a sequence of parameters.