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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:41:58+00:00 2026-05-28T03:41:58+00:00

I am facing huge issues with running date-related queries on the local SQLite database,

  • 0

I am facing huge issues with running date-related queries on the local SQLite database, kindly appreciate your help on this!

I am trying to retrieve all events that have its eventdate within a given year and month. To do this, I calculated the first date and last date of that month, then queried where the event date is within this range.

My test data uses:
String[] testEventDates = {“2011-12-17”, “2012-02-25”, “2012-01-10”, “2012-01-01”,
“2011-12-24″,”2012-02-25”}

But when I run my query below, I get ALL of these entries retrieved, which is wrong. My query is, (SELECT…WHERE) “eventDate >= ” + firstDay.

When I add the other limit, ” AND “eventDate < ” + lastDay,
it also works weirdly, the cursor is null instead.

Please help me if you have any clues about this!

// fetch events by month, between first and last days calculated
public Cursor fetchEventsByMonth(int month, int year) {
    String firstDay, lastDay, correctedMonth;

    if (month == 12) {
        lastDay = Integer.toString(year+1) + "-01-01";
    } else{
        if (month+1 < 10) {
            correctedMonth = "0" + Integer.toString(month+1);
            } else correctedMonth = Integer.toString(month+1);
        lastDay = Integer.toString(year) + "-" + correctedMonth
                + "-01";
    }
    if (month < 10) {
        correctedMonth = "0" + Integer.toString(month);
        } else correctedMonth = Integer.toString(month);
    firstDay = Integer.toString(year) + "-" + correctedMonth + "-01";
    firstDay = "2012-02-25";

    Cursor mCursor =
    mDb.query(DATABASE_TABLE_EVENTS, new String[] { KEY_EID, KEY_ENAME,
            KEY_EINTERESTS, KEY_EBENS, KEY_EDATE, KEY_ETIME, KEY_EVHO, KEY_EABBR,
            KEY_ELOCATION, KEY_ETYPE, KEY_ENATURE, KEY_ESTATUS, 
            KEY_ESIGNEDUP, KEY_ECAPACITY}, "eventDate >= " + firstDay
            // + " AND "eventDate < " + lastDay
                    , null, null, null,
            "eventDate");
    if (mCursor != null) mCursor.moveToFirst();

    return mCursor;
}   
  • 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-28T03:41:58+00:00Added an answer on May 28, 2026 at 3:41 am

    Have you tried to reproduce the query directly? (I mean querying the database with some tools like SQLite Manager).

    NB: when you are using STRINGS you have ALWAYS to use single quote!
    I also modified it to use selection and selection args, so you are much security safer and it do the job of inserting the single quote automatically. Try it
    So your query have to be:

    Cursor mCursor =
        mDb.query(DATABASE_TABLE_EVENTS, new String[] { KEY_EID, KEY_ENAME,
                KEY_EINTERESTS, KEY_EBENS, KEY_EDATE, KEY_ETIME, KEY_EVHO, KEY_EABBR,
                KEY_ELOCATION, KEY_ETYPE, KEY_ENATURE, KEY_ESTATUS, 
                KEY_ESIGNEDUP, KEY_ECAPACITY}, "eventDate >= ? AND eventDate < ?"
                        , new String[] { firstDay, lastDay}, null, null,
                "eventDate");
        if (mCursor != null) mCursor.moveToFirst();
    

    Also a really big advice is to never store dates and times with strings. First of all because they are static in this way, instead you have to think that people live in different countries with different times and timezones so you have to think in a worldwide way 🙂

    The best way to store dates and times is (in my opinion) with a long (time stamp).
    So when you retrieve a date/time you can pass directly it to a Date constructor or a Calendar constructor. Also it’s much, much much easier to compare dates in that way and it’s really much faster than String compare 🙂

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

Sidebar

Related Questions

Am facing few issues with multi step transction in hibernate. This is what I
I am facing an application designed to import huge amounts of data into a
I'm facing a problem with IE6. I took the toggle function from this website
I am facing strange issue on Windows CE: Running 3 EXEs 1)First exe doing
im facing the challange of optimizing a database design by an automated mechanism. the
I´m facing a huge problem since a couple weeks. I´ve an asp.net application hosted
I'm facing a huge problem here which is driving me crazy, I've made an
i am facing some problem with files with huge data. i need to skip
I'm currently facing huge problem i.e I'm showing Image and some text in a
i am facing a huge problem with the Cannot access a disposed object. Object

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.