garten

view db.c @ 17:5e6c9260913a

lots of cleanups and small fixes
author meillo@marmaro.de
date Wed, 23 Jul 2008 17:14:38 +0200
parents 3c104b5f5158
children 5937504619f2
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "db.h"
4 #include "game.h"
8 void db_connect() {
9 if (sqlite3_open(database, &db) != SQLITE_OK) {
10 fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
11 sqlite3_close(db);
12 exit(1);
13 }
14 }
17 void db_close() {
18 sqlite3_close(db);
19 }
22 sqlite3_stmt* db_query(char* query) {
23 int error;
24 sqlite3_stmt* stmt;
26 error = sqlite3_prepare(db, query, -1, &stmt, NULL);
27 if (error) {
28 fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
29 }
30 return stmt;
31 }
35 int db_update(char* query) {
36 int error;
37 sqlite3_stmt* stmt;
39 error = sqlite3_prepare(db, query, -1, &stmt, NULL);
40 if (error || sqlite3_step(stmt) != SQLITE_DONE) {
41 fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
42 return 0;
43 }
44 return 1;
45 }
48 int db_rows(sqlite3_stmt* stmt) {
49 int i = 0;
51 sqlite3_reset(stmt);
52 while (sqlite3_step(stmt) == SQLITE_ROW) {
53 i++;
54 }
55 sqlite3_reset(stmt);
56 return i;
57 }
60 int read_time() {
61 sprintf(query, "select time from game;");
62 stmt = db_query(query);
63 if (sqlite3_step(stmt) != SQLITE_ROW) {
64 printf("error: %s\n", sqlite3_errmsg(db));
65 exit(1);
66 }
67 gametime = sqlite3_column_int(stmt, 0);
68 sqlite3_finalize(stmt);
69 return gametime;
70 }
73 void db_error(char* info) {
74 fprintf(stderr, "%s%sSQL error: %s\n", (info) ? info : "", (info) ? ": " : "", sqlite3_errmsg(db));
75 }