Santo Shakil
Santo Shakil

Reputation: 1052

How to update iterable values into sqflite table in Flutter

I need an example of How to update iterable values into sqflite table in Flutter. I have created a data table and insert iterable values into this table.

Future callLogDB() async {
    Iterable<CallLogEntry> cLog = await CallLog.get();
    final dbHelper = DatabaseHelper.instance;

    cLog.forEach((log) async {
      // row to insert
      Map<String, dynamic> row = {
        DatabaseHelper.columnName: '${log.name}',
        DatabaseHelper.columnNumber: '${log.number}',
        DatabaseHelper.columnType: '${log.callType}',
        DatabaseHelper.columnDate:
            '${DateTime.fromMillisecondsSinceEpoch(log.timestamp)}',
        DatabaseHelper.columnDuration: '${log.duration}'
      };
      await dbHelper.insert(row);
      print('CallLog:: $row');
    });
    return cLog;
  }

Here is the insert method:

Future<int> insert(Map<String, dynamic> row) async {
    Database db = await instance.database;
    return await db.insert(table, row);
  }

And here is the update method:

Future<int> update(Map<String, dynamic> row) async {
    Database db = await instance.database;
    int id = row[columnId];
    return await db.update(table, row, where: '$columnId = ?', whereArgs: [id]);
  }

I want to update this table with the same iterable values. how can I do it?

Upvotes: 0

Views: 264

Answers (1)

Santo Shakil
Santo Shakil

Reputation: 1052

At last, I found the solution of this problem.

Future callLogDB() async {
Iterable<CallLogEntry> cLog = await CallLog.get();
final dbHelper = DatabaseHelper.instance;

cLog.toList().asMap().forEach((cLogIndex, log) async {
  // row to insert
  Map<String, dynamic> row = {
    DatabaseHelper.columnId: cLogIndex,
    DatabaseHelper.columnName: '${log.name}',
    DatabaseHelper.columnNumber: '${log.number}',
    DatabaseHelper.columnType: '${log.callType}',
    DatabaseHelper.columnDate:
        '${DateTime.fromMillisecondsSinceEpoch(log.timestamp)}',
    DatabaseHelper.columnDuration: '${log.duration}'
  };
  await dbHelper.update(row);
  print('CallLog $cLogIndex:: $row');
});
return cLog;

}

Upvotes: 1

Related Questions