Is that easy to read an exist sqlite.db file without much effort ?
I mean if there's already a sqlite.db which include some data, for example I saved countries data in a sqlite.db, there's only a table named countries, and columns like id, countryName, capital,telephoneCode and so on.
One of this requirement: is there easy way to override the column name like table name override DataClassName
If you have an existing table structure, the easiest way to use that with moor are the new .moor files introduced in 1.7. You can just put CREATE TABLE statements in there, include them from a @UseMoor or @UseDao annotation and you're good to go.
To view the create table statements, just open your existing sqlite file somewhere (you can just import them to sqliteonline.com), run SELECT sql FROM sqlite_master WHERE name='countries'; and paste the result into a .moor file.
If you want to declare the table in Dart, moor also gives you pretty much full control over how the table is structured: You can override the tableName for the whole table and use named(columnName) on individual columns.
Do the mechanisms mentioned give you enough control to represent your existing sqlite table in moor? I'll close this issue for know, but please let me know if you need more information.
Ah, I can solve my problem with code bellow:
@UseMoor(include: {'countries.moor'})
class DictDatabase extends _$DictDatabase {
DictDatabase._()
: super(FlutterQueryExecutor(
path: filePathToCountriesSqliteDB,
logStatements: isDebugMoorDB,
));
static final DictDatabase instance = DictDatabase._();
@override
int get schemaVersion => 1;
}
One thing maybe worth mentioned is I update moor_flutter: ^1.7.0 from moor_flutter: ^1.6._
but forget update moor_generator: ^1.7.1 from moor_generator: ^1.6._ at the beginning and cause error. But works after I notice this.
Another one is I didn't rename the column yet. Because I notice the table is generated in __.g.dart. So I see without need to create content like bellow, it already works.
class DBCountries extends Table {
}
So for column name, do I need have to create the class DBCountries extends Table.. with named(columnName) to rename Column ?
Yeah the moor/moor_generator version mismatches can be annoying. We could get rid of them by specifying better constraints in our pubspec. But when there is some delay between uploading a new moor version and a new moor_generator version, the version can't be resolved at all and we get massive scoring penalties on pub.
So I see without need to create content like bellow, it already works
Yeah that's the idea of .moor files - you don't need to write Dart classes for your tables. The named on the column constructor just tells moor another name for your column: By default, we just use the snake_case of your Dart getter name as column name, but that can be overridden. For instance,
class DBCountries extends Table {
IntColumn get myFancyColumn => integer()(); // will be named my_fancy_column in the table
}
class DbCountries extends Table {
// will be named myFancyColumn in the table
IntColumn get myFancyColumn => integer().named('myFancyColumn')();
}
So this has nothing to do with renaming columns, you just use a different name from the beginning. If you had a table that contained a column named "myFancyColumn" (in sql), there would be no way to create a matching moor column otherwise, you'd have to use named.
I think if you can provide some log like āthereās new version of moor_generate ..**ā when run āflutter package getā. That would helpful enough.
Sent with GitHawk