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 923833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:15:15+00:00 2026-05-15T19:15:15+00:00

In handling a little SQL formatting I was amazed to find I could chain

  • 0

In handling a little SQL formatting I was amazed to find I could chain string formatters:

def get_sql(table, limit=True):
    sql = "select report_date from %s"
    if limit:
        result = "%s limit 1" % sql % table
    else:
        result = sql % table
    return result

Is this legit? Any reason not to do this?

  • 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-05-15T19:15:16+00:00Added an answer on May 15, 2026 at 7:15 pm

    It makes sense that it works because a statement like this:

    'some value goes here %s' % value

    Actually returns a string. It’s probably a bit more logical to view it like this:

    result = ("%s limit 1" % sql) % table

    There’s nothing expressly wrong with doing that, but chaining operators can lead to problems with figuring out where an error came from.

    So for instance, this works fine:

    >>> sql = 'a value of %s'
    >>> x = 'some string %s with stuff'
    >>> y = 'VALUE'
    >>> x % sql % y
    'some string a value of VALUE with stuff'
    

    But if there was a formatting error in there (I realize this example is pathological, but it gets the point across):

    >>> sql = 'a value of %d'
    >>> x = 'some string %d with stuff'
    >>> y = 123    
    >>> x % sql % y
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: %d format: a number is required, not str
    

    It’s really not clear which %d is causing your error. For that reason, I would split it out and just use one % formatter per line if possible because then the traceback will be able to point you to exactly which line and which formatter had the issue.

    For the record, by doing it one formatter per line you’ll also make life a lot easier for anyone else who has to read your code and try to figure out what’s going on.

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

Sidebar

Related Questions

I need a little help creating a catch-all error handling page in my ICEfaces
I'm playing around with error handling and got a little problem. I connect with
Im having a little problem on handling touches in my apps. I set my
I'm handling RemoteIO to get mic inputs and modify them little. Sometimes it crashes
I just developed a little code to create a 24x60 table. I want to
I'm a little confused of the handling between the java Date Class and GregorianCalendar
I am a little confused on how to Error handling in Struts2. I wish
i am getting little bit lost with all the XML handling methods in java,
Little question about Ruby error handling. I have some code that roughly resembles the
I'm attempting to learn a little more about handling sockets and network connections in

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.