2010-07-19 5 views
0

Я пытаюсь выбрать данные из базы данных и у меня есть следующий код на месте: Код:SQLite Проблема - Путь к базе данных

// 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); 

я получаю сообщение об ошибке EXC_BAD_ACCESS на первой IF оператор. Любые мысли.

Также в документах NSLOGDir и databasePath пусты.

У меня есть следующий код в 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]; 
+0

Опубликовать вывод 'NSLog ("% @ ", databasePath);' –

+0

Похоже, что существует проблема с документамиDir, здесь происходит ошибка EXC_BAD_ACCESS, и ничего не отображается в консоли. – Stephen

+0

Я немного изменил код, и теперь я получаю путь к базе данных, но он не будет проходить через следующий оператор IF: if (sqlite3_open ([databasePath UTF8String] и база данных) == SQLITE_OK) – Stephen

ответ

1

Похоже DataBasePath не удерживается, так между временем, что вид нагрузка и коды базы данных выполняются, это уже было освобождено. Если вы хотите сохранить эти переменные вокруг, вам необходимо изменить две последние строки этого:

documentsDir = [[documentPaths objectAtIndex:0] retain]; 
databasePath = [[documentsDir stringByAppendingPathComponent:databaseName] retain]; 

И так как вы делаете это в viewDidLoad вы должны освободить их в viewDidUnload (набор к нулю в этом методе в качестве колодец) и dealloc.

+0

Спасибо, что сработали. – Stephen

Смежные вопросы