Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UPDATE a BLOB field in an existing SQLite record,floor change it to String #652

Closed
HXiaoMing opened this issue Mar 25, 2022 · 5 comments
Closed
Assignees

Comments

@HXiaoMing
Copy link

the entity is:
@entity(tableName: 'Task')
class Task {
final Uint8List ext;
Task(this.ext);
}

the dao is :
@dao
abstract class TaskDao {
@query('update Task set ext = :ext')
Future updateTaskExt(Uint8List ext);
}

i want to update column ext, ext type is Uint8List,database type is blob,after execute TaskDao method updateTaskExt,ext value change to String!!!

@dkaera
Copy link
Collaborator

dkaera commented Jun 20, 2022

Please double it check. For me, following configuration works fine:

@Database(version: 1, entities: [Task])
abstract class FlutterDatabase extends FloorDatabase {
  TaskDao get taskDao;
}

@dao
abstract class TaskDao {

  @Query('UPDATE Task set ext = :ext')
  Future<void> updateTaskExt(Uint8List ext);
}

@entity
class Task {
  @PrimaryKey(autoGenerate: true)
  final int? id;

  final String message;

  final Uint8List ext;

  Task(this.id, this.message, this.ext);

}

generates next code:

class _$TaskDao extends TaskDao {
  _$TaskDao(this.database, this.changeListener)
      : _queryAdapter = QueryAdapter(database);

  final sqflite.DatabaseExecutor database;

  final StreamController<String> changeListener;

  final QueryAdapter _queryAdapter;

  @override
  Future<void> updateTaskExt(Uint8List ext) async {
    await _queryAdapter
        .queryNoReturn('UPDATE Task set ext = ?1', arguments: [ext]);
  }
}

@dkaera
Copy link
Collaborator

dkaera commented Jul 27, 2022

Close due to lack of answer from the creator of the question. Looks like the question might be solved by the suggestion in the comment.

@dkaera dkaera closed this as completed Jul 27, 2022
@HXiaoMing
Copy link
Author

HXiaoMing commented Sep 13, 2022

Please double it check. For me, following configuration works fine:

@Database(version: 1, entities: [Task])
abstract class FlutterDatabase extends FloorDatabase {
  TaskDao get taskDao;
}

@dao
abstract class TaskDao {

  @Query('UPDATE Task set ext = :ext')
  Future<void> updateTaskExt(Uint8List ext);
}

@entity
class Task {
  @PrimaryKey(autoGenerate: true)
  final int? id;

  final String message;

  final Uint8List ext;

  Task(this.id, this.message, this.ext);

}

generates next code:

class _$TaskDao extends TaskDao {
  _$TaskDao(this.database, this.changeListener)
      : _queryAdapter = QueryAdapter(database);

  final sqflite.DatabaseExecutor database;

  final StreamController<String> changeListener;

  final QueryAdapter _queryAdapter;

  @override
  Future<void> updateTaskExt(Uint8List ext) async {
    await _queryAdapter
        .queryNoReturn('UPDATE Task set ext = ?1', arguments: [ext]);
  }
}

after updateTaskExt,try to read it, then will throw error.

@dkaera
Copy link
Collaborator

dkaera commented Sep 13, 2022

Hey @HXiaoMing
I did some investigation and sflite has the same problem. Honestly, using query to update the data is not the right way to go, but it is possible in some extreme cases as a workaround. For this particular case I would recommend you use rawUpdate. Sample of the code you can see in my PR to your repository.

PS: this issue brings me an idea that DatabaseExecutor should be accessible in DAO classes to use raw methods without global variables. Thanks 👍

@dkaera dkaera self-assigned this Sep 14, 2022
@SEGVeenstra
Copy link
Collaborator

Closing: Inactive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants