garten
view db.c @ 18:5937504619f2
rename game.h -> modules.h; added some error handling; and more
author | meillo@marmaro.de |
---|---|
date | Wed, 23 Jul 2008 17:40:55 +0200 |
parents | 5e6c9260913a |
children | 17b2bcc42d72 |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "db.h"
7 void db_connect() {
8 if (sqlite3_open(database, &db) != SQLITE_OK) {
9 fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
10 sqlite3_close(db);
11 exit(1);
12 }
13 }
16 void db_close() {
17 sqlite3_close(db);
18 }
21 sqlite3_stmt* db_query(char* query) {
22 int error;
23 sqlite3_stmt* stmt;
25 error = sqlite3_prepare(db, query, -1, &stmt, NULL);
26 if (error) {
27 fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
28 }
29 return stmt;
30 }
34 int db_update(char* query) {
35 int error;
36 sqlite3_stmt* stmt;
38 error = sqlite3_prepare(db, query, -1, &stmt, NULL);
39 if (error || sqlite3_step(stmt) != SQLITE_DONE) {
40 fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
41 return 0;
42 }
43 return 1;
44 }
47 int db_rows(sqlite3_stmt* stmt) {
48 int i = 0;
50 sqlite3_reset(stmt);
51 while (sqlite3_step(stmt) == SQLITE_ROW) {
52 i++;
53 }
54 sqlite3_reset(stmt);
55 return i;
56 }
59 int read_time() {
60 sprintf(query, "select time from game;");
61 stmt = db_query(query);
62 if (sqlite3_step(stmt) != SQLITE_ROW) {
63 printf("error: %s\n", sqlite3_errmsg(db));
64 exit(1);
65 }
66 gametime = sqlite3_column_int(stmt, 0);
67 sqlite3_finalize(stmt);
68 return gametime;
69 }
72 void db_error(char* info) {
73 fprintf(stderr, "%s%sSQL error: %s\n", (info) ? info : "", (info) ? ": " : "", sqlite3_errmsg(db));
74 }