2012-01-15 8 views
1

AppDelegate.mでこのexc_bad_accessの問題が発生しました 私がやりたいことは複数のSQLクエリを持つことです。複雑さを避けるために、私は2つのブロックを実行しました(より多くのブロックがあります)、両方ともSQLとは異なるテーブルをクエリしています。EXC_BAD_ACCESS SQLデータベース

-(void)readDataFromDatabase { 

    // Setup the database object 
    sqlite3 *database; 

    // Initialize the budgetobjects Array 
    Part1Array = [[NSMutableArray alloc] init]; 

    // 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 * from part1TBL"; 
     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 
       // You can add more rows based on your object 
       NSString *Part1_Name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
       NSString *Part1_Description = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 
       // Create a new Restaurant with the data from the database 
       Part1 *newPart1 = [[Part1 alloc] initWithName:Part1_Name description:Part1_Description]; 

       // Add the budgetobject to BudgetObjectsrantArray 
       [Part1Array addObject:newPart1]; 

      } 
     } 

     // Release the compiled statement from memory 
     sqlite3_finalize(compiledStatement); 

    } 
    sqlite3_close(database); 


    // Setup the database object 
    sqlite3 *database2; 

    // Initialize the budgetobjects Array 
    Part2Array = [[NSMutableArray alloc] init]; 

    // 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 * from part2TBL"; 
     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 
       // You can add more rows based on your object 
       NSString *Part2_Name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
       NSString *Part2_Description = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 
       // Create a new Restaurant with the data from the database 
       Part2 *newPart2 = [[Part2 alloc] initWithName:Part2_Name description:Part2_Description]; 

       // Add the budgetobject to BudgetObjectsrantArray 
       [Part2Array addObject:newPart2]; 

      } 
     } 

     // Release the compiled statement from memory 
     sqlite3_finalize(compiledStatement); 

    } 
    sqlite3_close(database2); 





} 



@end 

EXC_BAD_ACCESSが

sqlite3_close(database2); 
+2

Raw SQL ?!本当に?どのような時間の無駄。少なくとも、FMDBのような適切なラッパーを使用してください。さらに良いことに、Core DataはMac OS X、iOS、iCloudとの優れた統合性を備えています。今の時代、SQLを手渡しすることは、あなたが難しい方法でやっていることの確かな兆しです。 – bbum

答えて

3

最後に強調し、私はsqliteのに慣れていないんだけど、あなたのコードの問題は、あなたが「データベース2」を開くことはありませんしているということであるようです。代わりに、「データベース」を2回開きます。これはタイプミスのようです。

//(sqlite3_open([DatabasePathにUTF8Stringを]、& データベース)== SQLITE_OK){

....

sqlite3_close(データベース場合ユーザfilessytem からデータベースを開き);

...

//は、ユーザーからのデータベースを開き(sqlite3_open([DatabasePathにUTF8Stringを]、& データベース)== SQLITE_OK){// < ---これがなければならない場合 をfilessytem &データベース2 ...

sqlite3_close(データベース2)。

EDIT:database2を使用しようとしていたときにデータベースを使用している場所がいくつかあります。データベースを再利用したり、該当する場合は共有機能に抽出することも考えてください。

+0

残りのブロックは 'database2'を使うように更新する必要があります。 (あるいは単に 'database'ポインタを再利用して、' database2'を忘れてしまいます。) – Mat

+0

まだ学習中です。真剣にコアデータも勉強しています。ジョシュにも書き直したコードはいいですね!どうもありがとうございました!今はデバイス上で実行されています –