Reputation: 4811
I'm trying to select data from the database and I have the following code in place: Code:
// Setup the database object
sqlite3 *database;
// Init the animals Array
list = [[NSMutableArray alloc] init];
NSLog(@"documents path: ", documentsDir);
NSLog(@"database path: ", databasePath);
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "select route_name from Route";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
// Read the data from the result row
NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
// Add the animal object to the animals Array
//[list addObject:animal];
[list addObject:aName];
//[animal release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
I'm getting EXC_BAD_ACCESS error at the first IF statement. Anyone any thoughts.
Also in the NSLOG documentsDir and databasePath are blank.
I have the following code in ViewDidLoad:
-(void)viewDidLoad
{
// Setup some globals
databaseName = @"xxxxxxCoreData.sqlite";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsDir = [documentPaths objectAtIndex:0];
documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
Upvotes: 0
Views: 693
Reputation: 15706
It looks like databasePath is not retained, so between the time that the view loads and your database code is run, it's already been released. If you want to keep those variables around, you need to change the last two lines to this:
documentsDir = [[documentPaths objectAtIndex:0] retain];
databasePath = [[documentsDir stringByAppendingPathComponent:databaseName] retain];
And since you're doing this in viewDidLoad
you should release them in viewDidUnload
(set to nil in this method as well) and dealloc
.
Upvotes: 1