I have cities
table and trying to insert city, upon database creation. The table structure is pretty simple, it has just id
and name
column.
In onCreate
method of my database class, I create table with this command:
var tblCities = 'cities';
await db.execute('CREATE TABLE $tblCities (id INTEGER PRIMARY KEY, name TEXT)');
I have Cities
model class with fromMap
and toMap
methods.
There are about 350 cities, and I wanted to insert them in the table.
Q. What is the best and easy way to do that?
This comes in my mind:
for
loop to iterate entire listtoMap
methoddb.insert
method inside the loopI'm not sure, but this seem dumb approach so thinking about better and optimized solution...
That's how to use Flutter's DataTable widget. Basically, you need to define the list of columns and rows for the table. You can also handle data sorting, handle row selection, and customize the style of the table by passing the optional arguments. Adjusting the width of the columns and the table can be one of the most challenging things.
Flutter provides ListView.builder which can be used to generate dynamic content from external sources. There are four types of ListViews. ListView is the default constructor of a ListView class. A ListView takes the list of children and makes it scrollable. The builder () constructor constructs the repeating list of items.
ListView example Step 1: Create a new flutter project Go to the VSCode and hit the shortcut keys: cmd + shift + p and type the Flutter,... Step 2: Write the Stateful Widget Now, if you do not know how to write the Stateful Widget in Flutter, then check out my... Step 3: Add ListView.builder () ...
But, first, let us create a new app in the Visual Studio Code. Go to the VSCode and hit the shortcut keys: cmd + shift + p and type the Flutter, and it will show to create a new project option, and it creates a new flutter project in your specified folder.
As mentioned by @chunhunghan, you can use batch to insert bulk data.
Here's step by step guideline:
cities.json
(create csv file of data and use csv to json converter like this)cities.json
file in your assets
directoryDefine it in pubspec.yaml
like this:
assets:
- assets/cities.json
Paste this code inside onCreate
method of your database class (make sure its after table creation query)
Batch batch = db.batch();
String citiesJson = await rootBundle.loadString('assets/json/cities.json');
List citiesList = json.decode(citiesJson);
citiesList.forEach((val) {
//assuming you have 'Cities' class defined
Cities city = Cities.fromMap(val);
batch.insert(tblCities, city.toMap());
});
batch.commit();
That's it! :)
There is Batch support
To avoid ping-pong between dart and native code, you can use Batch:
batch = db.batch();
batch.insert('Test', {'name': 'item'});
batch.update('Test', {'name': 'new_item'}, where: 'name = ?', whereArgs: ['item']);
batch.delete('Test', where: 'name = ?', whereArgs: ['item']);
results = await batch.commit();
official example https://github.com/tekartik/sqflite/blob/master/sqflite/example/lib/batch_test_page.dart
In your case, for loop list with batch.insert command, it's easier to maintain
for simplicity syntax, use toMap, example
batch.insert("cities", city.toMap());
detail https://www.techiediaries.com/flutter-sqlite-crud-tutorial/
If you prefer rawInsert, please reference Insert multiple records in Sqflite
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With