-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask Manager.cpp
More file actions
89 lines (81 loc) · 2.84 KB
/
Task Manager.cpp
File metadata and controls
89 lines (81 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <sqlite3.h>
using namespace std;
// Função para executar comandos SQL
int executeSQL(sqlite3* db, const string& sql) {
char* errMsg = nullptr;
int rc = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &errMsg);
if (rc != SQLITE_OK) {
cerr << "Erro SQL: " << errMsg << endl;
sqlite3_free(errMsg);
}
return rc;
}
// Função para exibir tarefas armazenadas no banco de dados
int callback(void* NotUsed, int argc, char** argv, char** azColName) {
for (int i = 0; i < argc; i++) {
cout << azColName[i] << ": " << (argv[i] ? argv[i] : "NULL") << endl;
}
cout << "--------------------------" << endl;
return 0;
}
int main() {
sqlite3* db;
// Abre o banco de dados (ou cria se não existir)
int rc = sqlite3_open("tasks.db", &db);
if (rc) {
cerr << "Erro ao abrir banco de dados: " << sqlite3_errmsg(db) << endl;
return rc;
}
// Criar tabela de tarefas se não existir
string createTableSQL = "CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY, description TEXT, status TEXT);";
executeSQL(db, createTableSQL);
int option;
do {
// Menu de opções
cout << "\n1. Adicionar Tarefa";
cout << "\n2. Listar Tarefas";
cout << "\n3. Atualizar Tarefa";
cout << "\n4. Excluir Tarefa";
cout << "\n5. Sair";
cout << "\nEscolha uma opção: ";
cin >> option;
cin.ignore();
if (option == 1) {
// Adiciona uma nova tarefa
string description;
cout << "Descrição: ";
getline(cin, description);
string sql = "INSERT INTO tasks (description, status) VALUES ('" + description + "', 'Pendente');";
executeSQL(db, sql);
}
else if (option == 2) {
// Lista todas as tarefas
string sql = "SELECT * FROM tasks;";
sqlite3_exec(db, sql.c_str(), callback, 0, nullptr);
}
else if (option == 3) {
// Atualiza o status de uma tarefa existente
int id;
string status;
cout << "ID da tarefa: ";
cin >> id;
cin.ignore();
cout << "Novo status: ";
getline(cin, status);
string sql = "UPDATE tasks SET status = '" + status + "' WHERE id = " + to_string(id) + ";";
executeSQL(db, sql);
}
else if (option == 4) {
// Exclui uma tarefa do banco de dados
int id;
cout << "ID da tarefa: ";
cin >> id;
string sql = "DELETE FROM tasks WHERE id = " + to_string(id) + ";";
executeSQL(db, sql);
}
} while (option != 5); // Continua até que o usuário escolha sair
// Fecha o banco de dados ao encerrar o programa
sqlite3_close(db);
return 0;
}