Search code examples
androiddate-formatormlitesql-insert

ORMLite insert Date with SQL


I have set up an ORMLite database in my android project.

I have a class called RbFehler with the following field in it:

@DatabaseField(columnName = "DATUM_LA", dataType = DataType.DATE_STRING,
    format = "yyyy-MM-dd HH:mm:ss")
public Date datum_la;

I have some logic that imports the data from a text file where the SQL-insert statements are stored. One line would look like this:

INSERT INTO RbFehler (KATALOGART,SYST,SUBSYST,KOMP,BAUTEIL,BEZ,PSYST,PSUBSYST,
    PKOMP,PBAUTEIL,INAKTIV,GELOESCHT,DATUM_LA)
    VALUES ('VW','4F5AB211','0600','4F5AA900','0010','Venturirohr','4F5AB211',
        '0600','4F5AA900','0000','0','0',2012-06-04 08:08:08);

But i always get the following error when i call:

dao.execute RawNoArgs(line);

java.lang.Illegal Argument Exception: Field class java.sql.Date for field
    Field Type:name=datum_la,class=RbFehler is not valid for data
    persister com.j256.ormlite.field.types.DateStringType@416ed260

I have also tried to add " ' " around the date in the SQL-statement but get the same error. If I remove the date from the SQL and the class everything works fine.

Can anyone tell me what I am doing wrong? I only found questions like this: ormlite read Date as 'yyyy-MM-dd'


Solution

  • I figured it out:

    Make sure that you use the right Date object in Java:

    @DatabaseField(columnName = "DATUM_LA", dataType = DataType.DATE_STRING,
     format = "yyyy-MM-dd HH:mm:ss")
    public java.util.Date datum_la;
    

    And not the java.sql.Date.