annotate db.c @ 16:3c104b5f5158
fixed small bug
author |
meillo@marmaro.de |
date |
Wed, 23 Jul 2008 16:49:10 +0200 |
parents |
082566ce7d10 |
children |
5e6c9260913a |
rev |
line source |
meillo@15
|
1 #include <stdio.h>
|
meillo@15
|
2 #include <stdlib.h>
|
meillo@15
|
3 #include "db.h"
|
meillo@15
|
4 #include "game.h"
|
meillo@15
|
5
|
meillo@15
|
6
|
meillo@15
|
7
|
meillo@15
|
8 void db_connect() {
|
meillo@15
|
9 if (sqlite3_open(database, &db) != SQLITE_OK) {
|
meillo@15
|
10 fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
meillo@15
|
11 sqlite3_close(db);
|
meillo@15
|
12 exit(1);
|
meillo@15
|
13 }
|
meillo@15
|
14 }
|
meillo@15
|
15
|
meillo@15
|
16
|
meillo@15
|
17 void db_close() {
|
meillo@15
|
18 sqlite3_close(db);
|
meillo@15
|
19 }
|
meillo@15
|
20
|
meillo@15
|
21
|
meillo@15
|
22 sqlite3_stmt* db_query(char* query) {
|
meillo@15
|
23 int error;
|
meillo@15
|
24 sqlite3_stmt* stmt;
|
meillo@15
|
25
|
meillo@15
|
26 error = sqlite3_prepare(db, query, -1, &stmt, NULL);
|
meillo@15
|
27 if (error) {
|
meillo@15
|
28 fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
|
meillo@15
|
29 }
|
meillo@15
|
30 return stmt;
|
meillo@15
|
31 }
|
meillo@15
|
32
|
meillo@15
|
33
|
meillo@15
|
34
|
meillo@15
|
35 int db_update(char* query) {
|
meillo@15
|
36 int error;
|
meillo@15
|
37 sqlite3_stmt* stmt;
|
meillo@15
|
38
|
meillo@15
|
39 error = sqlite3_prepare(db, query, -1, &stmt, NULL);
|
meillo@15
|
40 if (error || sqlite3_step(stmt) != SQLITE_DONE) {
|
meillo@15
|
41 fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
|
meillo@15
|
42 return 0;
|
meillo@15
|
43 }
|
meillo@15
|
44 return 1;
|
meillo@15
|
45 }
|
meillo@15
|
46
|
meillo@15
|
47
|
meillo@15
|
48 int read_time() {
|
meillo@15
|
49 sprintf(query, "select time from game;");
|
meillo@16
|
50 stmt = db_query(query);
|
meillo@15
|
51 if (sqlite3_step(stmt) != SQLITE_ROW) {
|
meillo@15
|
52 printf("error: %s\n", sqlite3_errmsg(db));
|
meillo@15
|
53 exit(1);
|
meillo@15
|
54 }
|
meillo@15
|
55 gametime = sqlite3_column_int(stmt, 0);
|
meillo@15
|
56 sqlite3_finalize(stmt);
|
meillo@15
|
57 return gametime;
|
meillo@15
|
58 }
|
meillo@15
|
59
|