mirror of
https://gitlab.cvh-server.de/skrause/flashcards.git
synced 2026-03-16 07:50:15 +01:00
more stuff!
This commit is contained in:
@@ -35,10 +35,11 @@ database_create_tables (sqlite3 *db)
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
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,"
|
||||
"`id` INTEGER NOT NULL,"
|
||||
"`category` INTEGER NOT NULL,"
|
||||
"`title` TEXT NOT NULL,"
|
||||
"`answer` TEXT NOT NULL,"
|
||||
"`next_time` INTEGER NOT NULL DEFAULT (unixepoch()),"
|
||||
"PRIMARY KEY(`id` AUTOINCREMENT)"
|
||||
")";
|
||||
|
||||
@@ -77,8 +78,11 @@ database_save_category (sqlite3 *db, const char *c)
|
||||
printf ("Failed to execute statement: %s\n", sqlite3_errmsg (db));
|
||||
|
||||
rc = sqlite3_step (stmt);
|
||||
if (rc != SQLITE_OK)
|
||||
fprintf (stderr, "Failed to execute statement: %s\n", sqlite3_errmsg (db));
|
||||
if (rc != SQLITE_DONE)
|
||||
{
|
||||
fprintf (stderr, "Failed to execute statement: %s\n",
|
||||
sqlite3_errmsg (db));
|
||||
}
|
||||
sqlite3_finalize (stmt);
|
||||
}
|
||||
|
||||
@@ -118,7 +122,7 @@ database_delete_category (sqlite3 *db, const int id)
|
||||
else
|
||||
fprintf (stderr, "Failed to execute statement: %s\n", sqlite3_errmsg (db));
|
||||
rc = sqlite3_step (stmt);
|
||||
if (rc != SQLITE_OK)
|
||||
if (rc != SQLITE_DONE)
|
||||
fprintf (stderr, "Execution failed: %s\n", sqlite3_errmsg (db));
|
||||
sqlite3_finalize (stmt);
|
||||
|
||||
@@ -129,30 +133,34 @@ database_delete_category (sqlite3 *db, const int id)
|
||||
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)
|
||||
if (rc != SQLITE_DONE)
|
||||
fprintf (stderr, "Execution failed: %s\n", sqlite3_errmsg (db));
|
||||
sqlite3_finalize (stmt);
|
||||
}
|
||||
|
||||
void
|
||||
database_save_card (sqlite3 *db, const card c)
|
||||
database_save_card (sqlite3 *db, const int category, const char *title,
|
||||
const char *answer)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
fprintf (stdout, "%s: %s\n", c.task, c.solution);
|
||||
int rc = sqlite3_prepare_v2 (db, "INSERT INTO cards VALUES(?, ?, ?)", -1,
|
||||
&stmt, nullptr);
|
||||
int rc = sqlite3_prepare_v2 (
|
||||
db, "INSERT INTO cards (category, title, answer) VALUES(?, ?, ?)", -1,
|
||||
&stmt, nullptr);
|
||||
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);
|
||||
sqlite3_bind_int (stmt, 1, category);
|
||||
sqlite3_bind_text (stmt, 2, title, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text (stmt, 3, answer, -1, SQLITE_STATIC);
|
||||
}
|
||||
else
|
||||
fprintf (stderr, "Failed to execute statement: %s\n", sqlite3_errmsg (db));
|
||||
|
||||
sqlite3_step (stmt);
|
||||
rc = sqlite3_step (stmt);
|
||||
if (rc != SQLITE_DONE)
|
||||
fprintf (stderr, "Execution failed: %s\n", sqlite3_errmsg (db));
|
||||
sqlite3_finalize (stmt);
|
||||
}
|
||||
|
||||
@@ -162,8 +170,11 @@ database_load_cards (sqlite3 *db, const int category)
|
||||
GArray *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);
|
||||
int rc
|
||||
= sqlite3_prepare_v2 (db,
|
||||
"SELECT * FROM cards WHERE category = ? AND "
|
||||
"next_time <= unixepoch() ORDER BY next_time ASC",
|
||||
-1, &stmt, nullptr);
|
||||
if (rc == SQLITE_OK)
|
||||
sqlite3_bind_int (stmt, 1, category);
|
||||
else
|
||||
@@ -174,8 +185,10 @@ database_load_cards (sqlite3 *db, const int category)
|
||||
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));
|
||||
c.title = strdup ((char *)sqlite3_column_text (stmt, 2));
|
||||
c.answer = strdup ((char *)sqlite3_column_text (stmt, 3));
|
||||
c.next_time
|
||||
= g_date_time_new_from_unix_utc (sqlite3_column_int64 (stmt, 4));
|
||||
|
||||
g_array_append_val (cards, c);
|
||||
}
|
||||
@@ -184,6 +197,26 @@ database_load_cards (sqlite3 *db, const int category)
|
||||
return cards;
|
||||
}
|
||||
|
||||
void
|
||||
database_schedule_card (sqlite3 *db, const int id, GDateTime *next_time)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
int rc = sqlite3_prepare_v2 (
|
||||
db, "UPDATE cards SET next_time = ? WHERE id = ?", -1, &stmt, nullptr);
|
||||
if (rc == SQLITE_OK)
|
||||
{
|
||||
sqlite3_bind_int64 (stmt, 1, g_date_time_to_unix (next_time));
|
||||
sqlite3_bind_int (stmt, 2, id);
|
||||
}
|
||||
else
|
||||
fprintf (stderr, "Failed to execute statement: %s\n", sqlite3_errmsg (db));
|
||||
|
||||
rc = sqlite3_step (stmt);
|
||||
if (rc != SQLITE_DONE)
|
||||
fprintf (stderr, "Execution failed: %s\n", sqlite3_errmsg (db));
|
||||
sqlite3_finalize (stmt);
|
||||
}
|
||||
|
||||
void
|
||||
database_delete_card (sqlite3 *db, const int id)
|
||||
{
|
||||
@@ -195,6 +228,8 @@ database_delete_card (sqlite3 *db, const int id)
|
||||
else
|
||||
fprintf (stderr, "Failed to execute statement: %s\n", sqlite3_errmsg (db));
|
||||
|
||||
sqlite3_step (stmt);
|
||||
rc = sqlite3_step (stmt);
|
||||
if (rc != SQLITE_DONE)
|
||||
fprintf (stderr, "Execution failed: %s\n", sqlite3_errmsg (db));
|
||||
sqlite3_finalize (stmt);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user