Android上的ORMLite在创建包含枚举类型和默认值的列的表时会引发异常

时间:2011-10-06 17:53:31

标签: java android ormlite

我的一个业务对象获得了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)
     ...

1 个答案:

答案 0 :(得分:2)

垃圾。这是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中修复了它。 遗憾。