Douglas Mesquita
Douglas Mesquita

Reputation: 920

Sqlite query returns no value Android

I have a sql query that is not returning any value, but it has data to be returned. The following query code

Cursor cursor = db.query(CrimeDB.NOME_TABELA, CrimeDB.COLUNAS,
            CrimeDB.ID_CIDADE + "=" + idCidade + "" + " AND "
                    + CrimeDB.TIME + " >= datetime('" + dataInicioFormatada
                    + "') AND " + CrimeDB.TIME + " <= datetime('"
                    + dataFimFormatada + "')" + " AND "
                    + CrimeDB.GRUPO_CRIME + "=" + idCategoria + "", null,
            null, null, null);

Read cursor

if (cursor.moveToFirst()) {

        do {
            crime = new Crime();
            crime.setLastUpadateToken(ultimoTokenValido
                    .getUltimoTokenAtualizado());

            listCrime.add(itemCrime);

        } while (cursor.moveToNext());

    }

The query result is:

    SELECT    
        grupo_crime_id_grupo_crime, 
    id_crime, 
    cities_id_cities, 
    time 
FROM 
    crime 
WHERE 
    cities_id_cities=1650 AND 
    time >= datetime('20-10-2012') AND 
    time <= datetime('22-05-2014') AND 
    grupo_crime_id_grupo_crime=1

was returns to realize any value because there is value in the database. Using the SQLite Editor I see any register on the table crime.

id_crime | cities_id_cities | grupo_crime_id_grupo_crime | time
1          1650               1                            28-03-2013
2          1650               1                            06-04-2013

Upvotes: 0

Views: 370

Answers (3)

Chintan Soni
Chintan Soni

Reputation: 25267

Change the date format with yyyy/MM/dd and try using between instead of comparing dates with >= and <=.

Upvotes: 1

CL.
CL.

Reputation: 180010

The proper date format, both in the database and in your query parameters, must be yyyy-mm-dd.

Upvotes: 0

stinepike
stinepike

Reputation: 54672

use apostrophe (') while checking in where clause

Cursor cursor = db.query(CrimeDB.NOME_TABELA, CrimeDB.COLUNAS,
            CrimeDB.ID_CIDADE + "='" + idCidade + "'" + " AND "
                    + CrimeDB.TIME + " >= datetime('" + dataInicioFormatada
                    + "') AND " + CrimeDB.TIME + " <= datetime('"
                    + dataFimFormatada + "')" + " AND "
                    + CrimeDB.GRUPO_CRIME + "='" + idCategoria + "'", null,
            null, null, null);

Upvotes: 0

Related Questions