0

我的一个业务对象有一个 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

1 回答 1

2

废话。这是columnDefinition4.26 版本中引入的功能并在 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;

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

于 2011-10-06T20:41:57.853 回答