我正在使用 ormlite for android,我有以下模型
@DatabaseTable(tableName = DatabaseConstants.TABLE_ARTICLE)
public class ArticleModel {
@DatabaseField(generatedId = true, columnName = DatabaseConstants.ARTICLE_ID)
public int id;
@DatabaseField(columnName = DatabaseConstants.ARTICLE_NAME)
public String name = "";
@DatabaseField(columnName = DatabaseConstants.ARTICLE_IMAGE_URL)
public String imageUrl = "";
@DatabaseField(columnName = DatabaseConstants.ARTICLE_IMAGE,dataType= DataType.BYTE_ARRAY)
public byte[] image;
可以按如下方式更新图像:
ContentValues cv = new ContentValues();
cv.put(DatabaseConstants.ARTICLE_IMAGE, UtilBitmap.bitmapToByteArray(bitmap));
SQLiteDatabase sqLiteDatabase = this.baseHelper.getWritableDatabase();
sqLiteDatabase.update(DatabaseConstants.TABLE_ARTICLE, cv,
DatabaseConstants.ARTICLE_MOBILE_IMAGE_URL + "= ?", new String[]{url});
但是除了以下代码之外,是否有一个 ormlite 方法可以更新到字节数组:
updateBuilder.updateColumnValue(DatabaseConstants.ARTICLE_IMAGE,imageBytes);
updateBuilder.where().eq(DatabaseConstants.ARTICLE_MOBILE_IMAGE_URL, url);
问题是可以更新图像使用前一种方式而不使用后一种方式,即使用后一种方式时,图像无法正确存储在数据库中。我使用sqliteviewer检查图像是否已存储,后一种方法失败。有没有更好的方法使用“ormlite”更新字节数组(图像)?
I am using ormlite for android, I have following model
@DatabaseTable(tableName = DatabaseConstants.TABLE_ARTICLE)
public class ArticleModel {
@DatabaseField(generatedId = true, columnName = DatabaseConstants.ARTICLE_ID)
public int id;
@DatabaseField(columnName = DatabaseConstants.ARTICLE_NAME)
public String name = "";
@DatabaseField(columnName = DatabaseConstants.ARTICLE_IMAGE_URL)
public String imageUrl = "";
@DatabaseField(columnName = DatabaseConstants.ARTICLE_IMAGE,dataType= DataType.BYTE_ARRAY)
public byte[] image;
It is possible to update an image as follows:
ContentValues cv = new ContentValues();
cv.put(DatabaseConstants.ARTICLE_IMAGE, UtilBitmap.bitmapToByteArray(bitmap));
SQLiteDatabase sqLiteDatabase = this.baseHelper.getWritableDatabase();
sqLiteDatabase.update(DatabaseConstants.TABLE_ARTICLE, cv,
DatabaseConstants.ARTICLE_MOBILE_IMAGE_URL + "= ?", new String[]{url});
But is there is an ormlite method to update to byte array other than the follwing code:
updateBuilder.updateColumnValue(DatabaseConstants.ARTICLE_IMAGE,imageBytes);
updateBuilder.where().eq(DatabaseConstants.ARTICLE_MOBILE_IMAGE_URL, url);
The problem is that it is possible to update the image using the former way but not using the latter way, ie, when using the latter way , image is not properly stored in database . I used the sqliteviewer to check whether image is stored , and it fails with the latter method. is there any better approach to update the byte array (image) using "ormlite"?
发布评论
评论(1)
您缺少的是使用
SelectArg
功能 “http://ormlite.com/”rel="nofollow">ORMLite。当您看到要指定 SQL?
参数时,请使用SelectArg
。因此,您的代码将变成:SelectArg
还允许您构建更新语句,然后在执行该语句之前多次设置参数。因此,如果您重复更新某个字段,则不必多次构建相同的更新语句。What you are missing is using the
SelectArg
feature of ORMLite. You useSelectArg
when you seen to specify a SQL?
argument. So you code would turn into:SelectArg
also allows you to build your update statement and then set the argument multiple times before you execute the statement. So if you are repeatedly updating a certain field, you don't have to build the same update statement multiple times.