import 'dart:io'; import 'package:path_provider/path_provider.dart'; import 'package:rogapp/model/location.dart'; import 'package:sqflite/sqflite.dart'; import 'package:path/path.dart'; class DatabaseHelper{ DatabaseHelper._privateConstructor(); static final DatabaseHelper instance = DatabaseHelper._privateConstructor(); static Database? _database; Future get database async => _database ??= await _initDatabase(); Future _initDatabase() async { Directory documentDirectory = await getApplicationDocumentsDirectory(); String path = join(documentDirectory.path, 'rog.db'); // return await openDatabase( // path, // version: 1, // onCreate: _onCreate, // ); return openDatabase(join(await getDatabasesPath(), 'rog.db',), version: 1, onCreate: _onCreate); } Future _onCreate(Database db, int version) async { await db.execute(''' CREATE TABLE destination( location_id INTEGER PRIMARY KEY, location_name TEXT, category TEXT, zip TEXT, address TEXT, prefecture TEXT, area TEXT, city TEXT, latitude REAL, longitude REAL, photos TEXT, fax TEXT, email TEXT, facility TEXT, remark TEXT, tags TEXT, event_name TEXT, event_active INTEGER, hidden_location INTEGER, auto_checkin INTEGER, checkin_radius INTEGER, checkin_point INTEGER, buy_point INTEGER, evaluation_value TEXT, shop_closed INTEGER, shop_shutdown INTEGER, opening_hours_mon TEXT, opening_hours_tue TEXT, opening_hours_wed TEXT, opening_hours_thu TEXT, opening_hours_fri TEXT, opening_hours_sat TEXT, opening_hours_sun TEXT parammeters TEXT ) '''); } Future> getDestinations() async { Database db = await instance.database; var dest = await db.query('destination'); List destList = dest.isNotEmpty ? dest.map((e) => Location.fromMap(e)).toList() : []; print("--------- ${destList}"); return destList; } Future> getDestinationByLatLon(double lat, double lon) async { Database db = await instance.database; var dest = await db.query('destination', where: "lat = ${lat} and lon= ${lon}"); List destList = dest.isNotEmpty ? dest.map((e) => Location.fromMap(e)).toList() : []; return destList; } Future deleteDestination(int location_id) async { Database db = await instance.database; var dest = await db.delete('destination', where: "location_id = ${location_id}"); int ret = dest > 0 ? dest : -1; return ret; } FutureisAlreadyAvailable(int location_id) async{ Database db = await instance.database; var dest = await db.delete('destination', where: "location_id = ${location_id}"); return dest > 0 ? true : false; } Future insertDestination(Location dest) async { Database db = await instance.database; int res = await db.insert( 'destination', dest.toMap(), conflictAlgorithm: ConflictAlgorithm.replace, ); print("------ database helper insert ${res}-----------::::::::"); return res; } Future updateAction(Location destination, bool checkin)async { Database db = await instance.database; int act = checkin == false ? 0 : 1; Map row = { "checkedin": act }; return await db.update( "destination", row, where: 'location_id = ?', whereArgs: [destination.location_id] ); } }