sqlite 3 not entering step loop?
I decided to walk before I run, so I am trying to get a label to reflect a simple username it pulls from the database.
However, it never enters the "step" loop because the preparation function does not result in SQLITE_OK.
What exactly is wrong with my database procedure if I can't even get it to be "SQLITE_OK" status?
//
// MainScreen.m
// DatabaseTest
//
// Created by User on 2/28/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "MainScreen.h"
#import <sqlite3.h>
@implementation MainScreen
@synthesize MyLabel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"AuditSosa.sqlite"];
}
- (IBAction)ButtonPressed:(id)sender {
sqlite3 *database;
if (sqlite3_open([[self dataFilePath] UTF8String], &database) != SQLITE_OK)
{
sqlite3_close(database);
NSAssert(0,@"Failed to open database");
//MyLabel.text = @"failed to open";
}
NSString *query = @"SELECT Username FROM Users";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [query UTF8String],-1,&statement, nil) ==SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
char*value = (char *) sqlite3_column_text(statement, 0);
MyLabel.text = [NSString stringWithCString:value encoding:NSUTF8StringEncoding];
}
sqlite3_finalize(statement);
}
else
{
MyLabel.text = @"failed";
}
}
@end
iPad