I am trying to open a sqlite db in the viewDidLoad routine and trying to send a sql query through to the db, but the sqlite_step() fails every time. I am not sure what's wrong here. I am just trying this as a hello world attempt at sqlite3.
#import "RootViewController.h"
#import "sqlite3.h"
static sqlite3_stmt *statement = nil;
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *dbname = @"name.sqlite";
sqlite3 *database;
int success;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:dbname];
// Open the database. The database was prepared outside the application.
success = sqlite3_open([path UTF8String], &database);
if (success == SQLITE_OK) {
NSLog(@"database opening successful : %d", success);
} else {
// Even though the open failed, call close to properly clean up resources.
sqlite3_close(database);
NSLog(@"database opening failed : %d", success);
// Additional error handling, as appropriate...
}
if (statement == nil) {
const char *sql = "insert into name(nid, name) values(6, 'testname');";
success = sqlite3_prepare_v2(database, sql, -1, &statement, NULL);
NSLog(@"query prepare status: %d", success);
if (success != SQLITE_OK) {
}
}
success = sqlite3_step(statement); //things fail here, as all calls above return ok
NSLog(@"query step status: %d", success);
if (success != SQLITE_DONE) {
}
sqlite3_reset(statement);
sqlite3_close(database);
}
...
It would be great if anyone could point me out where I might be wrong. Thanks for your time already.