1 Commits

Author SHA1 Message Date
grata
a58721a6f4 crud 2023-09-28 13:57:21 +02:00
4 changed files with 204 additions and 70 deletions

View File

@@ -1,32 +1,39 @@
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
// Models
import 'package:progetto_m335_flutter/model/note.dart';
import 'package:progetto_m335_flutter/model/promemoria.dart';
import 'package:sqflite/sqflite.dart';
class NoteDatabase {
static final NoteDatabase instance = NoteDatabase._init();
static Database? _database;
// Zero args constructor needed to extend this class
NoteDatabase();
NoteDatabase._init();
Future<Database> get database async {
if (_database != null) return _database!;
_database = await _initDB('note.db');
return _database!;
}
Future<Database> _initDB(String filePath) async {
print("Initializing database");
final databasePath = await getDatabasesPath();
final path = join(databasePath, filePath);
return await openDatabase(path, version: 1, onCreate: _createDB);
}
Future _createDB(Database database, int version) async {
if (await isDatabaseExists()) {
print("Database already created");
deleteDatabase();
print("Database deleted");
}
await database.execute('''CREATE TABLE promemoria (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -56,6 +63,23 @@ class NoteDatabase {
}
Future fillDemoData(Database database, int version) async {
var nota = Note();
nota.setTitle("Nota 1");
nota.setCreationDate("2023-09-56");
nota.setLastModificationDate("2023-09-56");
nota.setArrayPromemoria("1,2,3,4,5");
nota.setDescription("Questo è un esempio di nota 1.");
await createNote(database, nota);
print(await readNote(1));
nota.setDescription("ciao");
await updateNote (nota);
print(await readNote (1));
// Add fake categories
await database.execute('''
INSERT INTO note (
@@ -68,7 +92,6 @@ class NoteDatabase {
'Nota 2',
'2023-09-28',
'2023-09-28',
'',
'Questo è un esempio di nota 2.'
)
''');
@@ -84,7 +107,6 @@ class NoteDatabase {
'Nota 2',
'2023-09-28',
'2023-09-28',
'',
'Questo è un esempio di nota 2.'
)
''');
@@ -105,7 +127,6 @@ class NoteDatabase {
'2023-09-27',
'2023-09-27',
'2023-10-05',
'',
'Questo è un esempio di promemoria 1.',
'Alta',
'Rosso'
@@ -128,7 +149,6 @@ class NoteDatabase {
'2023-09-28',
'2023-09-28',
'2023-10-10',
'',
'Questo è un esempio di promemoria 2.',
'Media',
'Verde'
@@ -156,34 +176,19 @@ class NoteDatabase {
}
Future<void> deleteDatabase() async {
try {
final databasePath = await getDatabasesPath();
final path = join(databasePath, 'note.db');
databaseFactory.deleteDatabase(path);
} catch (error) {
throw Exception('DbBase.deleteDatabase: $error');
}
}
Future<void> createNote(Database database, Note note) async {
await database.execute('''
INSERT INTO note (
title,
creationDate,
lastModificationDate,
arrayPromemoria,
description,
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);
) VALUES (
'$note.title}',
'$note.creationDate',
'$note.lastModificationDate',
'$note.arrayPromemoria.toString()',
'$note.description',
)
''');
print('note $note.title inserted');
// Restituisce true se il database è stato aperto correttamente, altrimenti false.
return database.isOpen;
}
Future<List<Map>> selectAllPromemoria() async {
@@ -202,29 +207,23 @@ class NoteDatabase {
return maps;
}
Future<void> createNote(Database database, Note note) async {
await database.insert(
'note',
note.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
print('note $note.title inserted');
}
Future<void> createPromemoria(
Database database, Promemoria promemoria) async {
await database.execute('''
INSERT INTO promemoria (
title,
creationDate,
lastModificationDate,
expirationDate,
arrayPromemoria,
description,
priority,
color
) VALUES (
'$promemoria.title',
'$promemoria.creationDate',
'$promemoria.lastModificationDate',
'$promemoria.expirationDate',
'$promemoria.arrayPromemoria.toString()',
'$promemoria.description',
'$promemoria.priority',
'$promemoria.color'
)
''');
await database.insert(
'promemoria',
promemoria.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
print('promemoria $promemoria.title inserted');
}
@@ -244,11 +243,17 @@ class NoteDatabase {
return results.first;
}
// Future<void> updatePromemoria(Promemoria promemoria) async {
// final db = await database;
Future<void> updatePromemoria(Promemoria promemoria) async {
final db = await database;
//await db.update('promemoria', promemoria, where: 'id = ?',
// Pass the Dog's id as a whereArg to prevent SQL injection.
// whereArgs: [dog.id],)
//}
await db.update('promemoria', promemoria.toMap(),
where: 'id = ?', whereArgs: [promemoria.getId()]);
}
Future<void> updateNote(Note note) async {
final db = await database;
await db.update('note', note.toMap(),
where: 'id = ?', whereArgs: [note.getId()]);
}
}

View File

@@ -9,4 +9,63 @@ class Note extends BaseEntity {
static String lastModificationDate = BaseEntity.getLastEditDate;
static String arrayPromemoria = '';
static String description = '';
Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'creationDate': creationDate,
'lastModificationDate': lastModificationDate,
'arrayPromemoria': arrayPromemoria,
'description': description
};
}
String getId() {
return id;
}
void setId(String id) {
id = id;
}
String getTitle() {
return title;
}
void setTitle(String title) {
title = title;
}
String getCreationDate() {
return creationDate;
}
void setCreationDate(String creationDate) {
creationDate = creationDate;
}
String getLastModificationDate() {
return lastModificationDate;
}
void setLastModificationDate(String lastModificationDate) {
lastModificationDate = lastModificationDate;
}
String getArrayPromemoria() {
return arrayPromemoria;
}
void setArrayPromemoria(String arrayPromemoria) {
arrayPromemoria = arrayPromemoria;
}
String getDescription() {
return description;
}
void setDescription(String description) {
description = description;
}
}

View File

@@ -13,6 +13,85 @@ class Promemoria extends BaseEntity {
static String arrayPromemoria = '';
static String description = '';
static Priority priority = Priority.none;
static Color color = Color.none;
Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'creationDate': creationDate,
'lastModificationDate': lastModificationDate,
'expirationDate': expirationDate,
'arrayPromemoria': arrayPromemoria,
'description': description,
'priority': priority,
'color': color,
};
}
String getId(){
return id;
}
void setId(String id) {
id = id;
}
String getTitle() {
return title;
}
void setTitle(String title) {
title = title;
}
String getCreationDate() {
return creationDate;
}
void setCreationDate(String creationDate) {
creationDate = creationDate;
}
String getLastModificationDate() {
return lastModificationDate;
}
void setLastModificationDate(String lastModificationDate) {
lastModificationDate = lastModificationDate;
}
String getExpirationDate() {
return expirationDate;
}
void setExpirationDate(String expirationDate) {
expirationDate = expirationDate;
}
String getArrayPromemoria() {
return arrayPromemoria;
}
void setArrayPromemoria(String arrayPromemoria) {
arrayPromemoria = arrayPromemoria;
}
String getDescription() {
return description;
}
void setDescription(String description) {
description = description;
}
Priority getPriority() {
return priority;
}
void setPriority(Priority priority) {
priority = priority;
}
}

View File

@@ -29,14 +29,6 @@ class _TestState extends State<Test> {
print("Data printed");
}
Future<void> _deletedatabase() async {
final db = await noteDatabase.database;
print("Deleting database");
await db.delete(noteTable);
print("Database deleted");
}
@override
Widget build(BuildContext context) {
return Scaffold(
@@ -44,8 +36,7 @@ class _TestState extends State<Test> {
child: Column(
children: [
FloatingActionButton(onPressed: _pressed),
FloatingActionButton(onPressed: _printdata),
FloatingActionButton(onPressed: _deletedatabase)
FloatingActionButton(onPressed: _printdata)
],
)
)