# HG changeset patch # User meillo@marmaro.de # Date 1217187294 -7200 # Node ID 17b2bcc42d728dcaa9b1790fb4c5b18b2b8fec5e # Parent eb8db0d906de9b79e923dd7f884c9830468e7a37 added check for empty result; minor stuff diff -r eb8db0d906de -r 17b2bcc42d72 db.c --- a/db.c Wed Jul 23 17:44:15 2008 +0200 +++ b/db.c Sun Jul 27 21:34:54 2008 +0200 @@ -4,6 +4,11 @@ +void db_error(char* info) { + fprintf(stderr, "%s%sSQL error: %s\n", (info) ? info : "", (info) ? ": " : "", sqlite3_errmsg(db)); +} + + void db_connect() { if (sqlite3_open(database, &db) != SQLITE_OK) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); @@ -57,18 +62,20 @@ int read_time() { + int error; + sprintf(query, "select time from game;"); stmt = db_query(query); - if (sqlite3_step(stmt) != SQLITE_ROW) { - printf("error: %s\n", sqlite3_errmsg(db)); + error = sqlite3_step(stmt); + if (error == SQLITE_DONE) { + fprintf(stderr, "no game available\n"); + exit(1); + } + if (error != SQLITE_ROW) { + db_error("read_time"); exit(1); } gametime = sqlite3_column_int(stmt, 0); sqlite3_finalize(stmt); return gametime; } - - -void db_error(char* info) { - fprintf(stderr, "%s%sSQL error: %s\n", (info) ? info : "", (info) ? ": " : "", sqlite3_errmsg(db)); -}