From c3a444f863ace2f4905d85778117c82b44188181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Thu, 28 Sep 2023 13:38:09 +0200 Subject: [PATCH] work in progress database --- lib/database/database.dart | 145 ++++++++++++++++++++++++++++++------- lib/main.dart | 5 +- lib/myApp.dart | 1 + 3 files changed, 123 insertions(+), 28 deletions(-) diff --git a/lib/database/database.dart b/lib/database/database.dart index 8d9aea3..098ea74 100644 --- a/lib/database/database.dart +++ b/lib/database/database.dart @@ -22,62 +22,55 @@ class NoteDatabase { } Future _initDB(String filePath) async { - // On Android, it is typically data/data//databases. - // On iOS and MacOS, it is the Documents directory. final databasePath = await getDatabasesPath(); - // Directory databasePath = await getApplicationDocumentsDirectory(); final path = join(databasePath, filePath); return await openDatabase(path, version: 1, onCreate: _createDB); } Future _createDB(Database database, int version) async { - //check if the database is created - if (database.query(noteTable) != null) { + if (await isDatabaseExists()) { print("Database already created"); + deleteDatabase(); + print("Database deleted"); + } - }else{ - print("demo data inserting"); - await database.execute('''CREATE TABLE promemoria ( + await database.execute('''CREATE TABLE promemoria ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, creationDate TEXT NOT NULL, lastModificationDate TEXT, expirationDate TEXT, + arrayPromemoria TEXT, description TEXT, priority TEXT, color TEXT ); '''); - await database.execute('''CREATE TABLE note ( + await database.execute('''CREATE TABLE note ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, creationDate TEXT NOT NULL, lastModificationDate TEXT, + arrayPromemoria TEXT, description TEXT ); '''); - print("database created"); - } - - + print("database created"); await fillDemoData(database, version); } + Future fillDemoData(Database database, int version) async { - - print("boh speriamo funzioni"); - // Add some fake accounts - - // Add fake categories await database.execute(''' INSERT INTO note ( title, creationDate, lastModificationDate, + arrayPromemoria, description ) VALUES ( 'Nota 2', @@ -93,6 +86,7 @@ class NoteDatabase { title, creationDate, lastModificationDate, + arrayPromemoria, description ) VALUES ( 'Nota 2', @@ -109,6 +103,7 @@ class NoteDatabase { creationDate, lastModificationDate, expirationDate, + arrayPromemoria, description, priority, color @@ -130,6 +125,7 @@ class NoteDatabase { creationDate, lastModificationDate, expirationDate, + arrayPromemoria, description, priority, color @@ -159,6 +155,50 @@ class NoteDatabase { } } + Future close() async { + final database = await instance.database; + database.close(); + } + + + Future deleteDatabase() async { + final databasePath = await getDatabasesPath(); + final path = join(databasePath, 'note.db'); + databaseFactory.deleteDatabase(path); + } + + Future 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 createNote(Database database, Note note) async { + await database.execute(''' + INSERT INTO note ( + title, + creationDate, + lastModificationDate, + arrayPromemoria, + description, + + ) VALUES ( + '$note.title}', + '$note.creationDate', + '$note.lastModificationDate', + '$note.arrayPromemoria.toString()', + '$note.description', + ) + '''); + + print('note $note.title inserted'); + } + Future> selectAllPromemoria() async { final db = await database; @@ -167,15 +207,68 @@ class NoteDatabase { return maps; } - Future close() async { - final database = await instance.database; - database.close(); + + Future> selectAllNotes() async { + final db = await database; + + final List> maps = await db.query(noteTable); + + return maps; } - // WARNING: FOR DEV/TEST PURPOSES ONLY!! - Future deleteDatabase() async { - final databasePath = await getDatabasesPath(); - final path = join(databasePath, 'note.db'); - databaseFactory.deleteDatabase(path); + + Future 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' + ) + '''); + + print('promemoria $promemoria.title inserted'); } + + + Future> readPromemoria(int id) async { + final db = await database; + + final results = await db.query('SELECT * FROM note where id=$id'); + return results.first; + } + + + Future> readNote(int id) async { + final db = await database; + + final results= await db.query('SELECT * FROM promemoria where id=$id'); + + return results.first; + } + +// Future 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],) +//} + + } + diff --git a/lib/main.dart b/lib/main.dart index 06709ad..9fac54f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; -import '../myApp.dart'; +import 'myApp.dart'; void main() { runApp(MyApp()); -} \ No newline at end of file + print("App started"); +} diff --git a/lib/myApp.dart b/lib/myApp.dart index 49d3545..311f7df 100644 --- a/lib/myApp.dart +++ b/lib/myApp.dart @@ -4,6 +4,7 @@ import 'navigation.dart'; class MyApp extends StatelessWidget { const MyApp({ Key? key }) : super(key: key); + @override Widget build(BuildContext context) { return MaterialApp(