LiamC
LiamC

Reputation: 106

Perform sqlite query and nothing happens

Strange one here. I have a database with a fixtures table which holds all the fixtures for a sports teams season. I am trying to create the functionality to allow the manager of the team to edit a fixture. I am able to select all the fixtures and return them to the listview but when I try to perform a query based upon the match_date column, nothing happens.

public class EditSchedule extends AppCompatActivity {

    public Button fixtureSearch;
    public EditText fixtureDate;
    public String searchDate;

    ListView editMatch;

    DBHelper dbHelper = new DBHelper(this);

    SQLiteDatabase sqLiteDatabase;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editfixture);

        editMatch = (ListView) findViewById(R.id.listViewEditMatch);
        fixtureDate = (EditText) findViewById(R.id.fixtureDateEditSearch);
        searchDate = fixtureDate.getText().toString();

        fixtureSearch = (Button) findViewById(R.id.fixtureSearchButton);
        fixtureSearch.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //populate listview with query result

                        //final Cursor cursor = dbHelper.getAllFixtures();
                        final Cursor cursor = dbHelper.getMatch(searchDate);

                        final FixtureAdapter fixtureAdapter = new FixtureAdapter(getApplicationContext(), cursor, 0);

                        editMatch.setAdapter(fixtureAdapter);

                    }
                    }
        );
    }

}

My getMatch method in DBHelper looks like this:

    public Cursor getMatch(String date) {
        SQLiteDatabase sqLiteDatabase = getWritableDatabase();

        String[] match = {_ID, MATCH_DATE, MATCH_OPPONENT, MATCH_VENUE, MATCH_TIME};

        return sqLiteDatabase.query(TABLE_FIXTURES, match, MATCH_DATE + " = '" + date + "'", null, null, null, null);
    }
}

There is nothing at all in the logcat when I click the search button, and you can see EditSchedule class that I have commented out the getAllFixtures cursor which does work. Your help would be greatly appreciated.

Upvotes: 1

Views: 172

Answers (1)

Sazzad Hissain Khan
Sazzad Hissain Khan

Reputation: 40136

I see that you are passing a datetime as String,

public Cursor getMatch(String date) { ... }
  1. You have to understand first SQLite have many format to store datetime in database. You must have to be concern about which format is stored in database and by which format you are searching with.
  2. When you search with date ist easier but when you search with date time for a equality comparison (using ==) its difficult because there are upto nanosecond precision limit which have to be matched when you are filtering.
  3. Its always a good practice to store datetime as long number and also search with a long which can be often obtained by System.currentTimeMillis().
  4. Please find some valuable information from the post here. It might help you anyway.

Upvotes: 1

Related Questions