garten
view db.c @ 16:3c104b5f5158
fixed small bug
author | meillo@marmaro.de |
---|---|
date | Wed, 23 Jul 2008 16:49:10 +0200 |
parents | 082566ce7d10 |
children | 5e6c9260913a |
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 read_time() {
49 sprintf(query, "select time from game;");
50 stmt = db_query(query);
51 if (sqlite3_step(stmt) != SQLITE_ROW) {
52 printf("error: %s\n", sqlite3_errmsg(db));
53 exit(1);
54 }
55 gametime = sqlite3_column_int(stmt, 0);
56 sqlite3_finalize(stmt);
57 return gametime;
58 }