garten

changeset 20:17b2bcc42d72 tip

added check for empty result; minor stuff
author meillo@marmaro.de
date Sun, 27 Jul 2008 21:34:54 +0200
parents eb8db0d906de
children
files db.c
diffstat 1 files changed, 14 insertions(+), 7 deletions(-) [+]
line diff
     1.1 --- a/db.c	Wed Jul 23 17:44:15 2008 +0200
     1.2 +++ b/db.c	Sun Jul 27 21:34:54 2008 +0200
     1.3 @@ -4,6 +4,11 @@
     1.4  
     1.5  
     1.6  
     1.7 +void db_error(char* info) {
     1.8 +	fprintf(stderr, "%s%sSQL error: %s\n", (info) ? info : "", (info) ? ": " : "", sqlite3_errmsg(db));
     1.9 +}
    1.10 +
    1.11 +
    1.12  void db_connect() {
    1.13  	if (sqlite3_open(database, &db) != SQLITE_OK) {
    1.14  		fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    1.15 @@ -57,18 +62,20 @@
    1.16  
    1.17  
    1.18  int read_time() {
    1.19 +	int error;
    1.20 +
    1.21  	sprintf(query, "select time from game;");
    1.22  	stmt = db_query(query);
    1.23 -	if (sqlite3_step(stmt) != SQLITE_ROW) {
    1.24 -		printf("error: %s\n", sqlite3_errmsg(db));
    1.25 +	error = sqlite3_step(stmt);
    1.26 +	if (error == SQLITE_DONE) {
    1.27 +		fprintf(stderr, "no game available\n");
    1.28 +		exit(1);
    1.29 +	}
    1.30 +	if (error != SQLITE_ROW) {
    1.31 +		db_error("read_time");
    1.32  		exit(1);
    1.33  	}
    1.34  	gametime = sqlite3_column_int(stmt, 0);
    1.35  	sqlite3_finalize(stmt);
    1.36  	return gametime;
    1.37  }
    1.38 -
    1.39 -
    1.40 -void db_error(char* info) {
    1.41 -	fprintf(stderr, "%s%sSQL error: %s\n", (info) ? info : "", (info) ? ": " : "", sqlite3_errmsg(db));
    1.42 -}