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

  • Home
  • SEARCH
  • 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 9022701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:34:57+00:00 2026-06-16T05:34:57+00:00

I have the following method in a python module which takes all default arguments,

  • 0

I have the following method in a python module which takes all default arguments, and generates a SQL query from those arguments in the following way:

Wondering, if there is a better (and elegant and pythonic) way to generate the above SQL query

def myquery(arg1=None, arg2=None, arg3=None):
    query = "SELECT column FROM table WHERE"
    params = []
    if arg1 is not None:
        query = query + " arg1 = " + arg1
        params.append(arg1)

    if arg2 is not None:
        if params is not None:
            query = query + "AND arg2 = " + arg2
        else:
            query = query + " arg 2 = " + arg2
        params.append(arg2)

    if arg3 is not None:
        if params is not None:
            query = query + "AND arg3 = " + arg3
        else:
            query = query + " arg 3 = " + arg3
        params.append(arg3)
    return query
  • 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-16T05:34:59+00:00Added an answer on June 16, 2026 at 5:34 am

    If i was gonna do this without an ORM I would do something like

    def QueryWhere(table,where={}):
        params = ["%s=?"%key for key in where.keys()] #take advantage of builtin query placeholders (?)
        c = db.cursor()
        c.execute("SELECT * FROM %s WHERE %s"%(table," AND ".join(params)),where.values())
        return c.fetchall()
    
    print QueryWhere("users",{"name":"johndoe","pass":"1c4$g"}) 
    

    something like that anyway … I would also recommend looking into using an ORM

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

Sidebar

Related Questions

I have the following method, which takes in the name of a file as
I have following method which I am using to load ActiveX control dynamically, Dim
I have the following method which is called when the value Edit Class... is
I have the following method: public static T ExecuteScalar<T>( string query, SqlConnection connection, params
I have the following questions about tweepy python module 1.I am trying to retrieve
I have some python module, which has a class ModuleClass and I can't modify
I'd like to write a method in python which dynamically reads a module and
In python 2.5, I have the following code in a module called modtest.py: def
I am using python twisted module to intercept headers. I have the following script:
I took the following python recipe from Activestate.org, then I simply added the method

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.