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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:51:43+00:00 2026-06-11T16:51:43+00:00

How can I pull data in mySQL by day using python date? Say I

  • 0

How can I pull data in mySQL by day using python date?
Say I want day1 and day2 ( or a day after day1 ) iterate for n times

So I need the date in “where” SQL statement to look like below list in each iteration (n times )

day1 >= '2012-01-01'  and  day2 < '2012-01-02'    ( n = 1 )
day1 >= '2012-01-02'  and  day2 < '2012-01-03'    ( n = 2 )
.
.
day1 >= yesterday    and day2  < today            ( n times ) 

.

Start_date = '2012-01-01'   <- How can I write this in python
End_date = Today()   <- and this 

So as to write:

for each iteration ..
    con.execute("select * from table where date >= day1 and date < day2" )
  • 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-11T16:51:45+00:00Added an answer on June 11, 2026 at 4:51 pm

    You need to datetime module:-

    import datetime
    start = datetime.date(2012,01,01) 
    next = start + datetime.date.resolution
    
    while next <= datetime.date.today():
        print start, next
    
        con.execute("""
            select * from table where date >= %s and date < %s
        """, (start, next))
    
        start = next
        next = start + datetime.date.resolution
    

    IMPORTANT NOTICE: I updated the answer to fix a serious problem. Never ever use string formatting (a.k.a. %) for building SQL queries since it is open to serious problems including SQL injection. Use Python-<db_driver> api where nearly all RDMBes offers the same syntax

    execute("select * from blah where x=%s AND y=%s", (x, y))
                                         ^       ^  ^
                                         1       1  2
    

    1] No quote,
    2] No string formatting

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

Sidebar

Related Questions

I'm using jQuery deferred objects so I can pull data from multiple sources ,
I am trying to get Ajaz to pull data from mysql using a php
I have data that I want to pull from a mySQL database and I
I'm trying to pull some data from MySQL using an Array that was fetch
I am using django to pull some data from the MySQL server I have.
How can i pull data from different tables(models) into view in yii . actually
I'm attempting to pull some data from a SQLite database so that I can
I'm trying to pull in data from a remote SQL Server. I can access
I'm using a forum software where you can pull up an avatar link using
I'm working on a query where I pull data from multiple tables using left

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.