diff options
Diffstat (limited to 'src/db/sqlite3/db.c')
-rw-r--r-- | src/db/sqlite3/db.c | 50 |
1 files changed, 44 insertions, 6 deletions
diff --git a/src/db/sqlite3/db.c b/src/db/sqlite3/db.c index e701140..91ac0eb 100644 --- a/src/db/sqlite3/db.c +++ b/src/db/sqlite3/db.c @@ -15,7 +15,8 @@ int create_tables(const char *path); // callback function prototypes -int count_callback(void *first_arg, int argc, char **argv, char **azColName); +int group_count_callback(void *first_arg, int argc, char **argv, char **azColName); +int entry_count_callback(void *first_arg, int argc, char **argv, char **azColName); int load_groups_callback(void *first_arg, int argc, char **argv, char **azColName); // globals @@ -56,7 +57,7 @@ Group *db_load_groups() { } // count rows in table - res = sqlite3_exec(db, count_query, count_callback, &output, &zErrMsg); + res = sqlite3_exec(db, count_query, group_count_callback, &output, &zErrMsg); if(res != SQLITE_OK) { fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); @@ -77,8 +78,39 @@ Group *db_load_groups() { } Entry *db_load_entries() { - // TODO - return NULL; + Entry *output; + sqlite3 *db; + const char *count_query = "SELECT COUNT(*) FROM 'Entry'"; + const char *query = "SELECT ROWID, * FROM 'Entry'"; + char *zErrMsg = NULL; // hold error from sqlite3_exec + int res; + + res = sqlite3_open(USER_DATA_PATH, &db); + if(res) { + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); + sqlite3_close(db); + return NULL; + } + + // count rows in table + res = sqlite3_exec(db, count_query, entry_count_callback, &output, &zErrMsg); + if(res != SQLITE_OK) { + fprintf(stderr, "SQL error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + return NULL; + } + + // do select query + callback_index = 0; + res = sqlite3_exec(db, query, load_groups_callback, &output, &zErrMsg); + if(res != SQLITE_OK) { + fprintf(stderr, "SQL error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + return NULL; + } + + sqlite3_close(db); + return output; } @@ -134,14 +166,20 @@ int create_tables(const char *path) { } // callback functions -int count_callback(void *first_arg, int argc, char **argv, char **azColName) { - printf("count: %d\n", argc); +int group_count_callback(void *first_arg, int argc, char **argv, char **azColName) { Group **groups = (Group **) first_arg; *groups = malloc(sizeof(Group) * argc); return 0; } +int entry_count_callback(void *first_arg, int argc, char **argv, char **azColName) { + Entry **entries = (Entry **) first_arg; + *entries = malloc(sizeof(Entry) * argc); + + return 0; +} + int load_groups_callback(void *first_arg, int argc, char **argv, char **azColName) { Group **groups = (Group **) first_arg; |