简体   繁体   中英

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'

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM