# HG changeset patch # User meillo@marmaro.de # Date 1216824356 -7200 # Node ID 082566ce7d1070e25b2f1daa63d7012a4bb53885 # Parent bbc214c6fa6f0bd5214685b50dedf8e6d4769dae added yet unversioned but needed file db.c diff -r bbc214c6fa6f -r 082566ce7d10 db.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/db.c Wed Jul 23 16:45:56 2008 +0200 @@ -0,0 +1,59 @@ +#include +#include +#include "db.h" +#include "game.h" + + + +void db_connect() { + if (sqlite3_open(database, &db) != SQLITE_OK) { + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); + sqlite3_close(db); + exit(1); + } +} + + +void db_close() { + sqlite3_close(db); +} + + +sqlite3_stmt* db_query(char* query) { + int error; + sqlite3_stmt* stmt; + + error = sqlite3_prepare(db, query, -1, &stmt, NULL); + if (error) { + fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); + } + return stmt; +} + + + +int db_update(char* query) { + int error; + sqlite3_stmt* stmt; + + error = sqlite3_prepare(db, query, -1, &stmt, NULL); + if (error || sqlite3_step(stmt) != SQLITE_DONE) { + fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); + return 0; + } + return 1; +} + + +int read_time() { + sprintf(query, "select time from game;"); + db_query(query); + if (sqlite3_step(stmt) != SQLITE_ROW) { + printf("error: %s\n", sqlite3_errmsg(db)); + exit(1); + } + gametime = sqlite3_column_int(stmt, 0); + sqlite3_finalize(stmt); + return gametime; +} +