我正在使用带有Androids集成sqlite数据库的ORMLite,我正在尝试使用CURRENT_TIMESTAMP
作为字段的默认值。
我试过了:
@DatabaseTable(tableName = "tableone")
public class TableOne {
@DatabaseField(generatedId = true, columnName = "id")
private int id;
@DatabaseField(columnName = "created", defaultValue = "CURRENT_TIMESTAMP")
private java.util.Date created;
public TableOne() {
}
}
生成的ormlite_config.txt:
# --table-start--
dataClass=com.test.ormlite.TableOne
tableName=tableone
# --table-fields-start--
# --field-start--
fieldName=id
columnName=id
generatedId=true
# --field-end--
# --field-start--
fieldName=created
columnName=created
defaultValue=CURRENT_TIMESTAMP
# --field-end--
# --table-fields-end--
# --table-end--
#################################
但是我得到了一个带有以下stacktrace的SQLException:
W/System.err: java.sql.SQLException: Problems with field FieldType:name=created,class=TableOne parsing default date-string 'CURRENT_TIMESTAMP' using 'yyyy-MM-dd HH:mm:ss.SSSSSS'
at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
at com.j256.ormlite.field.types.DateStringType.parseDefaultString(DateStringType.java:46)
at com.j256.ormlite.field.FieldType.assignDataType(FieldType.java:1067)
at com.j256.ormlite.field.FieldType.<init>(FieldType.java:274)
at com.j256.ormlite.table.DatabaseTableConfig.convertFieldConfigs(DatabaseTableConfig.java:236)
at com.j256.ormlite.table.DatabaseTableConfig.extractFieldTypes(DatabaseTableConfig.java:101)
at com.j256.ormlite.dao.BaseDaoImpl.initialize(BaseDaoImpl.java:153)
at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:128)
at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:119)
at com.j256.ormlite.dao.BaseDaoImpl$5.<init>(BaseDaoImpl.java:921)
at com.j256.ormlite.dao.BaseDaoImpl.createDao(BaseDaoImpl.java:921)
at com.j256.ormlite.dao.DaoManager.doCreateDao(DaoManager.java:359)
at com.j256.ormlite.dao.DaoManager.createDaoFromConfig(DaoManager.java:326)
at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:55)
at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.getDao(OrmLiteSqliteOpenHelper.java:279)
at com.fibus.ormlite.database.ORMOpenDatabaseHelper.getTableOneDao(ORMOpenDatabaseHelper.java:47)
at com.fibus.ormlite.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5272)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
Caused by: java.text.ParseException: Unparseable date: "CURRENT_TIMESTAMP" (at offset 0)
at java.text.DateFormat.parse(DateFormat.java:571)
at com.j256.ormlite.field.types.BaseDateType.normalizeDateString(BaseDateType.java:47)
at com.j256.ormlite.field.types.DateStringType.parseDefaultString(DateStringType.java:44)
... 28 more
我还尝试将格式设置为'yyyy-MM-dd HH:mm:ss'
以匹配CURRENT_TIMESTAMP
的格式,并将dataType指定为DataType.DATE_STRING
,但结果相同。
在我看来,ormlite正在尝试将字符串CURRENT_TIMESTAMP
解析为日期,因为该文档说:
默认值
我们在表中创建新行时字段的字符串默认值。默认为none。
如果是这种情况,是否有办法阻止这种或其他方式设置默认值?我也尝试了version = true
但我不想在更新行时更新字段。
我搜索了SO和网络,但无法找到解决此问题的方法。
Android版2.2
ORMLite版本4.48
SQLite版本3.6.22
答案 0 :(得分:0)
您可以解决自定义容量问题:http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_5.html#Custom-Persisters。从现有的一个开始 - com.j256.ormlite.field.typesDateType,并更新它的parseDefaultString()方法来处理CURRENT_TIMESTAMP字符串并在java端设置当前时间,而不是sqlite端