Error getting the dates in query SQLite

5

Hello it happens that I had problem to obtain the records of the date field of a sqlite table, now I have corrected my code to obtain dates like this:

do
    {
     inicio=new Entidades.TablaInicio(inicio);
     inicio.setId(cursor.getInt(cursor.getColumnIndex(_inicio_Utilidades.id)));
     inicio.setNombre(cursor.getString(cursor.getColumnIndex(_inicio_Utilidades.Nombre)));
     /*------- Fecha -----------*/
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
     try {
         inicio.setFecha(sdf.parse(cursor.getString(cursor.getColumnIndex(_inicio_Utilidades.Fecha))));
     } catch (ParseException e) {
         //Error formato no adecuado.
     }
     listinicio.add(inicio);
   }

The problem is that he throws me this error

ATAL EXCEPTION: main Process: com.example.home.sb, PID: 9936 java.lang.NullPointerException at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1009) at java.text.DateFormat.parse

right on this line

     inicio.setFecha(sdf.parse(cursor.getString(cursor.getColumnIndex(_inicio_Utilidades.Fecha))));
     

Thanks for reading. I do this in a fragment.

    
asked by Geek 22.09.2018 в 20:09
source

2 answers

0

I notice several details to comment, this before commenting how to extract the data.

First I notice that you want to add the ID and Name values to your table in addition to the date:

inicio.setId(cursor.getInt(cursor.getColumnIndex(_inicio_Utilidades.id)));       
inicio.setNombre(cursor.getString(cursor.getColumnIndex(_inicio_Utilidades.Nombre)));
inicio.setFecha(dateFormat.parse(cursor.getString(cursor.getColumnIndex(_inicio_Utilidades.Fecha))));

for this the creation script of your table must be different, you must add the necessary fields to the structure, do it this way:

//public static final String crearTablaInicio = " create table " + TablaI + "(" +fecha + " Date ");
public static final String crearTablaInicio = " create table " + TablaI + "(id INTEGER PRIMARY KEY, nombre TEXT, " +fecha + " Date ");

You can not insert or get the other id and name fields if they do not exist in your table.

Now the query to obtain the values using a Select , if your field is called "idU" you must ensure that this is the name defined when creating the table.

It is important to mention that you do not need to add quotation marks as a container ('') since it is NOT a field type TEXT , it is INTEGER , if it is a field INTEGER , this should change.

//String query="Select * from " + _inicio_Utilidades.TablaInicio +" where idU= '"+idU+"'";
String query="Select * from " + _inicio_Utilidades.TablaInicio +" where idU= "+idU;

Now to extract the value of your table and add it to the field type Date of your object you can use the class DateFormat and use its method parse () to convert to Date :

...
...
   do
    {
     inicio=new Entidades.TablaInicio(inicio);
     inicio.setId(cursor.getInt(cursor.getColumnIndex(_inicio_Utilidades.id)));
     inicio.setNombre(cursor.getString(cursor.getColumnIndex(_inicio_Utilidades.Nombre)));
     /*------- Fecha -----------*/
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
     try {
         inicio.setFecha(sdf.parse(cursor.getString(cursor.getColumnIndex(_inicio_Utilidades.Fecha))));
     } catch (ParseException e) {
         //Error formato no adecuado.
     }
     listinicio.add(inicio);
   }
...
...
    
answered by 26.09.2018 / 19:41
source
0

You must use the getString and then format it, it can work for you:

String dateTime = row.getString(row.getColumnIndexOrThrow(COLUMN_INDEX));

To format it:

DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try 
{
    date = iso8601Format.parse(dateTime);
} 
catch (ParseException e) 
{
    Log.e(TAG, "Parsing ISO8601 datetime failed", e);
}

Or with SimpleDateFormat :

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date d=new Date();
try 
{
    d=  dateFormat.parse(dateTime);
} 
catch (ParseException e) 
{
        e.printStackTrace();
}

And on your cursor:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
do 
{
    inicio=new Entidades.TablaInicio(inicio);              
    inicio.setId(cursor.getInt(cursor.getColumnIndex(_inicio_Utilidades.id)));       
    inicio.setNombre(cursor.getString(cursor.getColumnIndex(_inicio_Utilidades.Nombre)));
    <---mas campos-->
    inicio.setFecha(dateFormat.parse(cursor.getString(cursor.getColumnIndex(_inicio_Utilidades.Fecha))));
    listinicio.add(inicio);
} while (cursor.moveToNext());
    
answered by 22.09.2018 в 20:41