garten

diff db.c @ 15:082566ce7d10

added yet unversioned but needed file db.c
author meillo@marmaro.de
date Wed, 23 Jul 2008 16:45:56 +0200
parents
children 3c104b5f5158
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/db.c	Wed Jul 23 16:45:56 2008 +0200
     1.3 @@ -0,0 +1,59 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include "db.h"
     1.7 +#include "game.h"
     1.8 +
     1.9 +
    1.10 +
    1.11 +void db_connect() {
    1.12 +	if (sqlite3_open(database, &db) != SQLITE_OK) {
    1.13 +		fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    1.14 +		sqlite3_close(db);
    1.15 +		exit(1);
    1.16 +	}
    1.17 +}
    1.18 +
    1.19 +
    1.20 +void db_close() {
    1.21 +	sqlite3_close(db);
    1.22 +}
    1.23 +
    1.24 +
    1.25 +sqlite3_stmt* db_query(char* query) {
    1.26 +	int error;
    1.27 +	sqlite3_stmt* stmt;
    1.28 +
    1.29 +	error = sqlite3_prepare(db, query, -1, &stmt, NULL);
    1.30 +	if (error) {
    1.31 +		fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
    1.32 +	}
    1.33 +	return stmt;
    1.34 +}
    1.35 +
    1.36 +
    1.37 +
    1.38 +int db_update(char* query) {
    1.39 +	int error;
    1.40 +	sqlite3_stmt* stmt;
    1.41 +
    1.42 +	error = sqlite3_prepare(db, query, -1, &stmt, NULL);
    1.43 +	if (error || sqlite3_step(stmt) != SQLITE_DONE) {
    1.44 +		fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
    1.45 +		return 0;
    1.46 +	}
    1.47 +	return 1;
    1.48 +}
    1.49 +
    1.50 +
    1.51 +int read_time() {
    1.52 +	sprintf(query, "select time from game;");
    1.53 +	db_query(query);
    1.54 +	if (sqlite3_step(stmt) != SQLITE_ROW) {
    1.55 +		printf("error: %s\n", sqlite3_errmsg(db));
    1.56 +		exit(1);
    1.57 +	}
    1.58 +	gametime = sqlite3_column_int(stmt, 0);
    1.59 +	sqlite3_finalize(stmt);
    1.60 +	return gametime;
    1.61 +}
    1.62 +