import 'package:flutter/material.dart'; import 'package:sqflite/sqflite.dart'; import 'package:path/path.dart'; import 'dart:async'; import 'datamodel.dart'; class DatabaseConnector{ static const String eventDatabase = 'sensor_events.db'; static const String eventTable = 'events'; dynamic database; Future<void> connect() async { database = openDatabase( join(await getDatabasesPath(), eventDatabase), // When the database is first created, create a table to store the events. onCreate: (db, version) { // Run the CREATE TABLE statement on the database. return db.execute( 'CREATE TABLE $eventTable(id INTEGER PRIMARY KEY, urnId INTEGER,urn TEXT, label TEXT, type TEXT, typeId INTEGER, description TEXT, status TEXT, startDate TEXT, endDate TEXT, latitude REAL, longitude REAL, elevation REAL)', ); }, // Set the version. This executes the onCreate function and provides a // path to perform database upgrades and downgrades. version: 1, ); } Future<void> insertEvent(Event event) async { final db = await database; await db.insert( eventTable, event.toMap(), conflictAlgorithm: ConflictAlgorithm.replace, ); } // A method that retrieves all the dogs from the dogs table. Future<List<Event>> getEvents() async { // Get a reference to the database. final db = await database; final List<Map<String, dynamic>> maps = await db.query(eventTable); // Convert the List<Map<String, dynamic> into a List<Event>. return List.generate(maps.length, (i) { return Event( id: maps[i]['id'], urnId: maps[i]['urnId'], urn: maps[i]['urn'], label: maps[i]['label'], type: maps[i]['type'], typeId: maps[i]['typeId'], description: maps[i]['description'], status: maps[i]['status'], startDate: maps[i]['startDate'], endDate: maps[i]['endDate'], latitude: maps[i]['latitude'], longitude: maps[i]['longitude'], elevation: maps[i]['elevation'], ); }); } } /* * int id; // Device URN id String urn; String label; String type; // Event type name TODO: this should be an EventType variable int typeId; String description; String status; String startDate; String endDate; String latitude; String longitude; String elevation; * * * */