sqlcool 2.7.0 sqlcool: ^2.7.0 copied to clipboard
A library to work with Sqflite providing a simple api for crud operations and reactivity bindings
import 'package:flutter/material.dart';
import 'package:sqlcool/sqlcool.dart';
import 'pages/select_bloc.dart';
import 'pages/index.dart';
import 'pages/join_query.dart';
import 'pages/upsert.dart';
import 'pages/sync_map.dart';
import 'conf.dart';
void main() {
/// initialize the database async. We will use the [onReady]
/// callback later to react to the initialization completed event
initDb();
runApp(MyApp());
}
Future<void> initDb() async {
// define the tables
DbTable category = DbTable("category")..varchar("name", unique: true);
DbTable product = DbTable("product")
..varchar("name", unique: true)
..integer("price")
..foreignKey("category", onDelete: OnDelete.cascade)
..index("name");
// prepare the queries
List<String> schemaQueries = category.queries..addAll(product.queries);
List<String> populateQueries = <String>[
'INSERT INTO category(name) VALUES("Category 1")',
'INSERT INTO category(name) VALUES("Category 2")',
'INSERT INTO category(name) VALUES("Category 3")',
'INSERT INTO product(name,price,category_id) VALUES("Product 1", 50, 1)',
'INSERT INTO product(name,price,category_id) VALUES("Product 2", 30, 1)',
'INSERT INTO product(name,price,category_id) VALUES("Product 3", 20, 2)'
];
List<String> queries = schemaQueries..addAll(populateQueries);
// initialize the database
String dbpath = "items.sqlite";
await db
.init(path: dbpath, queries: queries, verbose: true)
.catchError((dynamic e) {
throw ("Error initializing the database: ${e.message}");
});
}
final routes = {
'/': (BuildContext context) => PageIndex(),
'/select_bloc': (BuildContext context) => PageSelectBloc(),
'/join': (BuildContext context) => PageJoinQuery(),
'/upsert': (BuildContext context) => UpsertPage(),
'/sync_map': (BuildContext context) => SyncMapPage(),
};
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Sqlcool example',
routes: routes,
);
}
}