work in progress database
This commit is contained in:
@@ -1,39 +1,32 @@
|
|||||||
import 'package:path/path.dart';
|
import 'package:path/path.dart';
|
||||||
import 'package:sqflite/sqflite.dart';
|
|
||||||
|
|
||||||
// Models
|
// Models
|
||||||
import 'package:progetto_m335_flutter/model/note.dart';
|
import 'package:progetto_m335_flutter/model/note.dart';
|
||||||
import 'package:progetto_m335_flutter/model/promemoria.dart';
|
import 'package:progetto_m335_flutter/model/promemoria.dart';
|
||||||
|
import 'package:sqflite/sqflite.dart';
|
||||||
|
|
||||||
class NoteDatabase {
|
class NoteDatabase {
|
||||||
static final NoteDatabase instance = NoteDatabase._init();
|
static final NoteDatabase instance = NoteDatabase._init();
|
||||||
static Database? _database;
|
static Database? _database;
|
||||||
|
|
||||||
// Zero args constructor needed to extend this class
|
|
||||||
NoteDatabase();
|
|
||||||
|
|
||||||
NoteDatabase._init();
|
NoteDatabase._init();
|
||||||
|
|
||||||
Future<Database> get database async {
|
Future<Database> get database async {
|
||||||
if (_database != null) return _database!;
|
if (_database != null) return _database!;
|
||||||
|
|
||||||
_database = await _initDB('note.db');
|
_database = await _initDB('note.db');
|
||||||
return _database!;
|
return _database!;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Database> _initDB(String filePath) async {
|
Future<Database> _initDB(String filePath) async {
|
||||||
|
print("Initializing database");
|
||||||
final databasePath = await getDatabasesPath();
|
final databasePath = await getDatabasesPath();
|
||||||
|
|
||||||
final path = join(databasePath, filePath);
|
final path = join(databasePath, filePath);
|
||||||
|
|
||||||
return await openDatabase(path, version: 1, onCreate: _createDB);
|
return await openDatabase(path, version: 1, onCreate: _createDB);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future _createDB(Database database, int version) async {
|
Future _createDB(Database database, int version) async {
|
||||||
if (await isDatabaseExists()) {
|
|
||||||
print("Database already created");
|
|
||||||
deleteDatabase();
|
|
||||||
print("Database deleted");
|
|
||||||
}
|
|
||||||
|
|
||||||
await database.execute('''CREATE TABLE promemoria (
|
await database.execute('''CREATE TABLE promemoria (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
@@ -62,7 +55,6 @@ class NoteDatabase {
|
|||||||
await fillDemoData(database, version);
|
await fillDemoData(database, version);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Future fillDemoData(Database database, int version) async {
|
Future fillDemoData(Database database, int version) async {
|
||||||
// Add fake categories
|
// Add fake categories
|
||||||
await database.execute('''
|
await database.execute('''
|
||||||
@@ -76,11 +68,11 @@ class NoteDatabase {
|
|||||||
'Nota 2',
|
'Nota 2',
|
||||||
'2023-09-28',
|
'2023-09-28',
|
||||||
'2023-09-28',
|
'2023-09-28',
|
||||||
|
'',
|
||||||
'Questo è un esempio di nota 2.'
|
'Questo è un esempio di nota 2.'
|
||||||
)
|
)
|
||||||
''');
|
''');
|
||||||
|
|
||||||
|
|
||||||
await database.execute('''
|
await database.execute('''
|
||||||
INSERT INTO note (
|
INSERT INTO note (
|
||||||
title,
|
title,
|
||||||
@@ -92,6 +84,7 @@ class NoteDatabase {
|
|||||||
'Nota 2',
|
'Nota 2',
|
||||||
'2023-09-28',
|
'2023-09-28',
|
||||||
'2023-09-28',
|
'2023-09-28',
|
||||||
|
'',
|
||||||
'Questo è un esempio di nota 2.'
|
'Questo è un esempio di nota 2.'
|
||||||
)
|
)
|
||||||
''');
|
''');
|
||||||
@@ -112,6 +105,7 @@ class NoteDatabase {
|
|||||||
'2023-09-27',
|
'2023-09-27',
|
||||||
'2023-09-27',
|
'2023-09-27',
|
||||||
'2023-10-05',
|
'2023-10-05',
|
||||||
|
'',
|
||||||
'Questo è un esempio di promemoria 1.',
|
'Questo è un esempio di promemoria 1.',
|
||||||
'Alta',
|
'Alta',
|
||||||
'Rosso'
|
'Rosso'
|
||||||
@@ -134,6 +128,7 @@ class NoteDatabase {
|
|||||||
'2023-09-28',
|
'2023-09-28',
|
||||||
'2023-09-28',
|
'2023-09-28',
|
||||||
'2023-10-10',
|
'2023-10-10',
|
||||||
|
'',
|
||||||
'Questo è un esempio di promemoria 2.',
|
'Questo è un esempio di promemoria 2.',
|
||||||
'Media',
|
'Media',
|
||||||
'Verde'
|
'Verde'
|
||||||
@@ -160,24 +155,16 @@ class NoteDatabase {
|
|||||||
database.close();
|
database.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Future<void> deleteDatabase() async {
|
Future<void> deleteDatabase() async {
|
||||||
final databasePath = await getDatabasesPath();
|
try {
|
||||||
final path = join(databasePath, 'note.db');
|
final databasePath = await getDatabasesPath();
|
||||||
databaseFactory.deleteDatabase(path);
|
final path = join(databasePath, 'note.db');
|
||||||
|
databaseFactory.deleteDatabase(path);
|
||||||
|
} catch (error) {
|
||||||
|
throw Exception('DbBase.deleteDatabase: $error');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> isDatabaseExists() async {
|
|
||||||
final databasePath = await getDatabasesPath();
|
|
||||||
final path = join(databasePath, 'note.db');
|
|
||||||
// Apre il database in modalità sola lettura.
|
|
||||||
Database database = await openDatabase(path, readOnly: true);
|
|
||||||
|
|
||||||
// Restituisce true se il database è stato aperto correttamente, altrimenti false.
|
|
||||||
return database.isOpen;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Future<void> createNote(Database database, Note note) async {
|
Future<void> createNote(Database database, Note note) async {
|
||||||
await database.execute('''
|
await database.execute('''
|
||||||
INSERT INTO note (
|
INSERT INTO note (
|
||||||
@@ -207,7 +194,6 @@ class NoteDatabase {
|
|||||||
return maps;
|
return maps;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Future<List<Map>> selectAllNotes() async {
|
Future<List<Map>> selectAllNotes() async {
|
||||||
final db = await database;
|
final db = await database;
|
||||||
|
|
||||||
@@ -216,9 +202,8 @@ class NoteDatabase {
|
|||||||
return maps;
|
return maps;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> createPromemoria(
|
||||||
Future<void> createPromemoria(Database database,
|
Database database, Promemoria promemoria) async {
|
||||||
Promemoria promemoria) async {
|
|
||||||
await database.execute('''
|
await database.execute('''
|
||||||
INSERT INTO promemoria (
|
INSERT INTO promemoria (
|
||||||
title,
|
title,
|
||||||
@@ -244,7 +229,6 @@ class NoteDatabase {
|
|||||||
print('promemoria $promemoria.title inserted');
|
print('promemoria $promemoria.title inserted');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Future<Map<String, Object?>> readPromemoria(int id) async {
|
Future<Map<String, Object?>> readPromemoria(int id) async {
|
||||||
final db = await database;
|
final db = await database;
|
||||||
|
|
||||||
@@ -252,23 +236,19 @@ class NoteDatabase {
|
|||||||
return results.first;
|
return results.first;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Future<Map<String, Object?>> readNote(int id) async {
|
Future<Map<String, Object?>> readNote(int id) async {
|
||||||
final db = await database;
|
final db = await database;
|
||||||
|
|
||||||
final results= await db.query('SELECT * FROM promemoria where id=$id');
|
final results = await db.query('SELECT * FROM promemoria where id=$id');
|
||||||
|
|
||||||
return results.first;
|
return results.first;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Future<void> updatePromemoria(Promemoria promemoria) async {
|
// Future<void> updatePromemoria(Promemoria promemoria) async {
|
||||||
// final db = await database;
|
// final db = await database;
|
||||||
|
|
||||||
//await db.update('promemoria', promemoria, where: 'id = ?',
|
//await db.update('promemoria', promemoria, where: 'id = ?',
|
||||||
// Pass the Dog's id as a whereArg to prevent SQL injection.
|
// Pass the Dog's id as a whereArg to prevent SQL injection.
|
||||||
// whereArgs: [dog.id],)
|
// whereArgs: [dog.id],)
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,14 @@ class _TestState extends State<Test> {
|
|||||||
print("Data printed");
|
print("Data printed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _deletedatabase() async {
|
||||||
|
final db = await noteDatabase.database;
|
||||||
|
|
||||||
|
print("Deleting database");
|
||||||
|
await db.delete(noteTable);
|
||||||
|
print("Database deleted");
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@@ -36,7 +44,8 @@ class _TestState extends State<Test> {
|
|||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
FloatingActionButton(onPressed: _pressed),
|
FloatingActionButton(onPressed: _pressed),
|
||||||
FloatingActionButton(onPressed: _printdata)
|
FloatingActionButton(onPressed: _printdata),
|
||||||
|
FloatingActionButton(onPressed: _deletedatabase)
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user