mirror of
https://gitlab.cvh-server.de/skrause/flashcards.git
synced 2026-03-16 07:50:15 +01:00
formatting, small changes
This commit is contained in:
176
src/database.c
176
src/database.c
@@ -2,51 +2,40 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
sqlite3 *database_connect(const char *path)
|
||||
{
|
||||
int rc;
|
||||
gchar *file;
|
||||
sqlite3 *database_connect(const char *path) {
|
||||
sqlite3 *db;
|
||||
|
||||
file = g_build_filename(path, "cards.db", NULL);
|
||||
gchar *file = g_build_filename(path, "cards.db", NULL);
|
||||
int rc = sqlite3_open(file, &db);
|
||||
|
||||
rc = sqlite3_open(file, &db);
|
||||
|
||||
if (rc)
|
||||
{
|
||||
if (rc) {
|
||||
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Opened database successfully\n");
|
||||
return nullptr;
|
||||
}
|
||||
fprintf(stdout, "Opened database successfully\n");
|
||||
g_free(file);
|
||||
|
||||
database_create_tables(db);
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
void database_close(sqlite3 *db)
|
||||
{
|
||||
sqlite3_close(db);
|
||||
}
|
||||
void database_close(sqlite3 *db) { sqlite3_close(db); }
|
||||
|
||||
void database_create_tables(sqlite3 *db)
|
||||
{
|
||||
int rc;
|
||||
void database_create_tables(sqlite3 *db) {
|
||||
sqlite3_stmt *stmt;
|
||||
const char *sql;
|
||||
|
||||
sql = "CREATE TABLE IF NOT EXISTS `cards` ("
|
||||
"`category` INTEGER NOT NULL,"
|
||||
"`task` TEXT NOT NULL,"
|
||||
"`solution` TEXT NOT NULL"
|
||||
")";
|
||||
const char *sql = "CREATE TABLE IF NOT EXISTS `cards` ("
|
||||
"`id` INTEGER NOT NULL,"
|
||||
"`category` INTEGER NOT NULL,"
|
||||
"`task` TEXT NOT NULL,"
|
||||
"`solution` TEXT NOT NULL,"
|
||||
"PRIMARY KEY(`id` AUTOINCREMENT)"
|
||||
")";
|
||||
|
||||
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
|
||||
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
|
||||
|
||||
if (rc != SQLITE_OK)
|
||||
{
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
@@ -59,10 +48,9 @@ void database_create_tables(sqlite3 *db)
|
||||
"PRIMARY KEY(`id` AUTOINCREMENT)"
|
||||
")";
|
||||
|
||||
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
|
||||
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
|
||||
|
||||
if (rc != SQLITE_OK)
|
||||
{
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
@@ -70,75 +58,86 @@ void database_create_tables(sqlite3 *db)
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
void database_save_category(sqlite3 *db, const char *c)
|
||||
{
|
||||
int rc;
|
||||
void database_save_category(sqlite3 *db, const char *c) {
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
fprintf(stdout, "%s\n", c);
|
||||
rc = sqlite3_prepare_v2(db, "INSERT INTO categories (name) VALUES(?)", -1, &stmt, 0);
|
||||
int rc = sqlite3_prepare_v2(db, "INSERT INTO categories (name) VALUES(?)", -1, &stmt, nullptr);
|
||||
|
||||
if (rc == SQLITE_OK)
|
||||
{
|
||||
if (rc == SQLITE_OK) {
|
||||
sqlite3_bind_text(stmt, 1, c, -1, SQLITE_STATIC);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
printf("Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_OK)
|
||||
{
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
GArray *database_load_categories(sqlite3 *db)
|
||||
{
|
||||
GArray *categories;
|
||||
int rc;
|
||||
GArray *database_load_categories(sqlite3 *db) {
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
categories = g_array_new(TRUE, FALSE, sizeof(category));
|
||||
GArray *categories = g_array_new(TRUE, FALSE, sizeof(category));
|
||||
int rc = sqlite3_prepare_v2(db, "SELECT * FROM categories", -1, &stmt, nullptr);
|
||||
|
||||
rc = sqlite3_prepare_v2(db, "SELECT * FROM categories", -1, &stmt, 0);
|
||||
|
||||
if (rc != SQLITE_OK)
|
||||
{
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW)
|
||||
{
|
||||
int id = sqlite3_column_int(stmt, 0);
|
||||
char *name = strdup((const char *)sqlite3_column_text(stmt, 1));
|
||||
|
||||
category c = {id, name};
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
category c;
|
||||
c.id = sqlite3_column_int(stmt, 0);
|
||||
c.name = strdup((char *) sqlite3_column_text(stmt, 1));
|
||||
g_array_append_val(categories, c);
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return categories;
|
||||
}
|
||||
|
||||
void database_save_card(sqlite3 *db, card c)
|
||||
{
|
||||
int rc;
|
||||
void database_delete_category(sqlite3 *db, const int id) {
|
||||
// delete all cards in the category
|
||||
sqlite3_stmt *stmt;
|
||||
int rc = sqlite3_prepare_v2(db, "DELETE FROM cards WHERE category = ?", -1, &stmt, nullptr);
|
||||
if (rc == SQLITE_OK) {
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
} else {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
// delete the category
|
||||
rc = sqlite3_prepare_v2(db, "DELETE FROM categories WHERE id = ?", -1, &stmt, nullptr);
|
||||
if (rc == SQLITE_OK) {
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
} else {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
void database_save_card(sqlite3 *db, card c) {
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
fprintf(stdout, "%s: %s\n", c.task, c.solution);
|
||||
rc = sqlite3_prepare_v2(db, "INSERT INTO cards VALUES(?, ?, ?)", -1, &stmt, 0);
|
||||
int rc = sqlite3_prepare_v2(db, "INSERT INTO cards VALUES(?, ?, ?)", -1, &stmt, nullptr);
|
||||
|
||||
if (rc == SQLITE_OK)
|
||||
{
|
||||
if (rc == SQLITE_OK) {
|
||||
sqlite3_bind_int(stmt, 0, c.category);
|
||||
sqlite3_bind_text(stmt, 1, c.task, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 2, c.solution, -1, SQLITE_STATIC);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
@@ -146,11 +145,40 @@ void database_save_card(sqlite3 *db, card c)
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
GArray *database_load_cards(sqlite3 *db)
|
||||
{
|
||||
GArray *cards;
|
||||
GArray *database_load_cards(sqlite3 *db, int category) {
|
||||
GArray *cards = g_array_new(TRUE, FALSE, sizeof(card));
|
||||
|
||||
cards = g_array_new(TRUE, FALSE, sizeof(card));
|
||||
sqlite3_stmt *stmt;
|
||||
int rc = sqlite3_prepare_v2(db, "SELECT * FROM cards WHERE category = ?", -1, &stmt, nullptr);
|
||||
|
||||
if (rc == SQLITE_OK) {
|
||||
sqlite3_bind_int(stmt, 1, category);
|
||||
} else {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
card c;
|
||||
c.id = sqlite3_column_int(stmt, 0);
|
||||
c.category = sqlite3_column_int(stmt, 1);
|
||||
c.task = strdup((char *) sqlite3_column_text(stmt, 2));
|
||||
c.solution = strdup((char *) sqlite3_column_text(stmt, 3));
|
||||
|
||||
g_array_append_val(cards, c);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return cards;
|
||||
}
|
||||
}
|
||||
|
||||
void database_delete_card(sqlite3 *db, const int id) {
|
||||
sqlite3_stmt *stmt;
|
||||
int rc = sqlite3_prepare_v2(db, "DELETE FROM cards WHERE id = ?", -1, &stmt, nullptr);
|
||||
if (rc == SQLITE_OK) {
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
} else {
|
||||
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
sqlite3_step(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user