change modes
This commit is contained in:
87
lib/utils/database_helper.dart
Normal file
87
lib/utils/database_helper.dart
Normal file
@ -0,0 +1,87 @@
|
||||
import 'dart:io';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:rogapp/model/destination.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<Database> get database async => _database ??= await _initDatabase();
|
||||
|
||||
Future<Database> _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,
|
||||
name TEXT,
|
||||
address TEXT,
|
||||
phone TEXT,
|
||||
email TEXT,
|
||||
webcontents TEXT,
|
||||
videos TEXT,
|
||||
category TEXT,
|
||||
series INTEGER,
|
||||
lat REAL,
|
||||
lon REAL,
|
||||
list_order INTEGER,
|
||||
photos TEXT,
|
||||
checkin_radious REAL,
|
||||
auto_checkin INTEGER
|
||||
)
|
||||
''');
|
||||
}
|
||||
|
||||
Future<List<Destination>> getDestinations() async {
|
||||
Database db = await instance.database;
|
||||
var dest = await db.query('destination');
|
||||
List<Destination> destList = dest.isNotEmpty ?
|
||||
dest.map((e) => Destination.fromMap(e)).toList() : [];
|
||||
print("--------- ${destList}");
|
||||
return destList;
|
||||
}
|
||||
|
||||
Future<List<Destination>> getDestinationByLatLon(double lat, double lon) async {
|
||||
Database db = await instance.database;
|
||||
var dest = await db.query('destination', where: "lat = ${lat} and lon= ${lon}");
|
||||
List<Destination> destList = dest.isNotEmpty
|
||||
? dest.map((e) => Destination.fromMap(e)).toList() : [];
|
||||
return destList;
|
||||
}
|
||||
|
||||
Future<int> 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;
|
||||
}
|
||||
|
||||
Future<int> insertDestination(Destination dest) async {
|
||||
Database db = await instance.database;
|
||||
int res = await db.insert(
|
||||
'destination',
|
||||
dest.toMap(),
|
||||
conflictAlgorithm: ConflictAlgorithm.replace,
|
||||
);
|
||||
return res;
|
||||
}
|
||||
|
||||
// Future<int?> getPending() async{
|
||||
// Database db = await instance.database;
|
||||
// return await Sqflite.firstIntValue(await db.rawQuery("SELECT COUNT(*) FROM incidents"));
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user