Working on a simple TimeTracker App. You start a timer, wait a certain time, stop the clock and save spent minutes to a SQLite3 database. All is working as expected, when I tap on save I get a confirmation that record was saved but I can't get the saved entries shown on the console. As seen in the screenshot below I have a show button which should show me all my saved entries in the console including timestamp but nothing happens when I tap the button.
I think the problem is related to the search query but I can't figure our whats wrong.
From my understanding the query is looking for the timestamp (column 0) and the spent minutes (column 1) but it doesn't work, please help.
NSLog(@"Timestamp: %s - Timing: %s", sqlite3_column_text(statement, 0), sqlite3_column_text(statement, 1));
SQLite database is really simple, I only want to save one value > spent minutes. Maybe the database is not created correctly??
Database Creation:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"timings.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath:databasePath] == NO)
{
const char *dbPath = [databasePath UTF8String];
if (sqlite3_open(dbPath, &timings) == SQLITE_OK)
{
char *errormsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS TIMINGS(ID INTEGER PRIMARY KEY AUTOINCREMENT, TIME INTEGER)";
if (sqlite3_exec(timings, sql_stmt, NULL, NULL, &errormsg) != SQLITE_OK)
{
self.statusLabel.text = @"Failed to create database";
}
sqlite3_close(timings);
}
else
{
self.statusLabel.text = @"Failed to open/create database.";
}
}
}
Show saved entries on console:
-(IBAction)showButton:(id)sender
{
if (sqlite3_open([databasePath UTF8String], &timings) == SQLITE_OK)
{
NSString *queryStatement = [NSString stringWithFormat:@"SELECT TIMESTAMP, TIME FROM TIMINGS"];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(timings, [queryStatement UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW) {
NSLog(@"Timestamp: %s - Timing: %s", sqlite3_column_text(statement, 0), sqlite3_column_text(statement, 1));
}
sqlite3_finalize(statement);
sqlite3_close(timings);
}
}
}
