Note that there are some explanatory texts on larger screens.

plurals
  1. POSQLite3 database not showing entries on console via NSLog (Saving data to the DB works)
    text
    copied!<p>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 <code>show button</code> which should show me all my saved entries in the console including timestamp but nothing happens when I tap the button.</p> <p>I think the problem is related to the search query but I can't figure our whats wrong.</p> <p>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.</p> <pre><code> NSLog(@"Timestamp: %s - Timing: %s", sqlite3_column_text(statement, 0), sqlite3_column_text(statement, 1)); </code></pre> <p>SQLite database is really simple, I only want to save one value > spent minutes. Maybe the database is not created correctly??</p> <p>Database Creation:</p> <pre><code>- (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, &amp;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, &amp;errormsg) != SQLITE_OK) { self.statusLabel.text = @"Failed to create database"; } sqlite3_close(timings); } else { self.statusLabel.text = @"Failed to open/create database."; } } } </code></pre> <p>Show saved entries on console:</p> <pre><code>-(IBAction)showButton:(id)sender { if (sqlite3_open([databasePath UTF8String], &amp;timings) == SQLITE_OK) { NSString *queryStatement = [NSString stringWithFormat:@"SELECT TIMESTAMP, TIME FROM TIMINGS"]; sqlite3_stmt *statement; if (sqlite3_prepare_v2(timings, [queryStatement UTF8String], -1, &amp;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); } } } </code></pre> <p><img src="https://i.stack.imgur.com/osUGD.jpg" alt="Screernshot"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload