Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7727455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:27:48+00:00 2026-06-01T05:27:48+00:00

I am looking for an sqlite3 command that let’s me select entries given by

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:27:49+00:00Added an answer on June 1, 2026 at 5:27 am

    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.

    my_query = '''SELECT val_1, val_2
                  FROM   my_table 
                  WHERE  val_1 IN ({:s});'''.format(",".join("?"*len(key_indexes)))
    # -> 'SELECT val_1, val_2 FROM my_table WHERE val_1 IN (?,?,?);'
    c.execute(myquery, ind).fetchall()
    

    Additionally:
    You didn’t directly ask about this, but the first for loop and call to execute() could be reduced to a single call to executemany().
    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.

    c.executemany('''INSERT INTO my_table VALUES (?,?);''', my_data)
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app leveraging Core Data SQLITE3 that works perfectly in the simulator.
Looking to move data from a table A to history table B every X
Looking at adding some data graphing to a new iPhone app in development (ala
I've been looking for a C++ SQL library implementation that is simple to hook
Suppose I have a search screen that is intended for looking up items. There
I am in looking for a buffer code for process huge records in tuple
I have a sqlite database that I want to open using sqlite3.exe. Now I
i am looking to execute the following commands within a shell script: $ sqlite3
In the begininng of my app, i am running a sqlite3 command on existing
I am looking for JQuery or any other ways to store data on client-side

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.