我的一个业务对象获得了CommunicationMedium类型的成员,该成员是一个枚举。

public enum CommunicationMedium {
    EMAIL,
    PHONE,
    MOBILE,
    FACSIMILE,
    HOMEPAGE
}


这样,便对称为CommunicationData的业务对象进行了注释。

@DatabaseTable(tableName="store_communication_data")
public class CommunicationData{

    public static final String STORE_ID ="store_id";
    public static final String TABLE_NAME = "store_communication_data";
    public static final String MEDIUM_COLUMN_NAME = "medium";
    public static final String VALUE_COLUMN_NAME = "value";

    @DatabaseFieldSimple(canBeNull=false, columnName=STORE_ID)
    @DatabaseFieldForeign(foreign=true)
    private Store store;

    @DatabaseFieldSimple(canBeNull=false, columnName=MEDIUM_COLUMN_NAME, defaultValue="CommunicationMedium.EMAIL")
    @DatabaseFieldOther(dataType=DataType.ENUM_STRING)
    private CommunicationMedium communicationMedium;

    @DatabaseFieldSimple(canBeNull=false, columnName=VALUE_COLUMN_NAME)
    private String value;
    ...
}


当访问数据库帮助程序时,第一次应创建CommunicationData类的表时引发以下异常。 medium列的默认值似乎有问题。

java.sql.SQLException: SQL statement failed: CREATE TABLE `store_communication_data` (`medium` __ormlite__ no default value string was specified , `store_id` INTEGER NOT NULL , `value` VARCHAR NOT NULL )
     at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
     at com.j256.ormlite.table.TableUtils.doStatements(TableUtils.java:458)
     at com.j256.ormlite.table.TableUtils.doCreateTable(TableUtils.java:436)
     at com.j256.ormlite.table.TableUtils.createTable(TableUtils.java:223)
     at com.j256.ormlite.table.TableUtils.createTable(TableUtils.java:53)
     ...
Caused by: java.sql.SQLException: Problems executing Android statement: CREATE TABLE `store_communication_data` (`medium` __ormlite__ no default value string was specified , `store_id` INTEGER NOT NULL , `value` VARCHAR NOT NULL )
     at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
     at com.j256.ormlite.android.AndroidCompiledStatement.runUpdate(AndroidCompiledStatement.java:71)
     at com.j256.ormlite.table.TableUtils.doStatements(TableUtils.java:452)
     ...
Caused by: android.database.sqlite.SQLiteException: near "string": syntax error: , while compiling: CREATE TABLE `store_communication_data` (`medium` __ormlite__ no default value string was specified , `store_id` INTEGER NOT NULL , `value` VARCHAR NOT NULL )
     at android.database.sqlite.SQLiteProgram.native_compile(Native Method)
     at android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:110)
     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
     at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1026)
     ...

最佳答案

废话此错误是在4.26版中随columnDefinition功能引入的,并已在4.27中修复。如果在未指定@DatabaseFieldOther的情况下使用columnDefinition批注,则会触发此不幸的错误。您现在可以通过添加columnDefinition值来解决@Flo:

@DatabaseFieldSimple(canBeNull = false, columnName = MEDIUM_COLUMN_NAME, defaultValue = "EMAIL")
@DatabaseFieldOther(dataType = DataType.ENUM_STRING, columnDefinition = "VARCHAR(100) DEFAULT 'EMAIL' NOT NULL")
private CommunicationMedium communicationMedium;


bug已添加到我们的错误跟踪系统中,我已在4.27版中对其进行了修复。
抱歉。

09-10 05:48