2012-04-17 80 views
1

在AppDelegate中我找回我的數據庫的路徑:未知錯誤而執行select語句

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentDirectory=[paths objectAtIndex:0]; 
self.sqlFile=[[NSString alloc]init]; 
self.sqlFile=[documentDirectory stringByAppendingPathComponent:@"AnimalData.sqlite"]; 

在我的視圖控制器之一,我必須從數據庫中選擇值,代碼:

NSString *trimString=[appDelegate.mainBreedSelected stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

NSString *selectSQL=[NSString stringWithFormat:@"select breed from AnimalsTable where mainBreed=\"%@\"",trimString]; 
sqlite3_stmt *selectStatement; 
const char *select_stmt=[selectSQL UTF8String]; 
if (sqlite3_open([appDelegate.sqlFile UTF8String],&database)==SQLITE_OK) 
{ 
    sqlite3_prepare_v2(database,select_stmt,-1,&selectStatement,NULL); 
    if (sqlite3_step(selectStatement)==SQLITE_DONE) 
    { 
     while(sqlite3_step(selectStatement) == SQLITE_ROW) 
     { 
      NSString *animalBreed = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 2)]; 
      NSLog(@"Breed:%@",animalBreed); 
     } 
    } 
    else 
     NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(database)); 
    sqlite3_finalize(selectStatement); 
    sqlite3_close(database); 
} 
else 
    NSLog(@"Database cannot be opened"); 

我得到的錯誤爲:

*終止應用程序由於uncaug ht異常'NSInternalInconsistencyException', 原因:'選擇時出錯。 '未知錯誤''

如何解決此問題?

+0

我做了另一個改變,看它是否會工作但我得到新的錯誤更改是NSString * selectSQL = [NSString stringWithFormat:@「從動物列表中選擇品種,其中mainBreed =%@」,trimString];新的錯誤是:***終止應用程序由於未捕獲的異常'NSInternalInconsistencyException',原因:'錯誤,同時刪除。 '沒有這樣的專欄:羊'' – anjum 2012-04-17 08:38:03

回答

1

我已經改變了代碼:

if (sqlite3_open([appDelegate.sqlFile UTF8String], &database) == SQLITE_OK) { 
    NSLog(@"%@",appDelegate.sqlFile); 
    NSString *trimString=[appDelegate.mainBreedSelected stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    NSString *select_sql=[NSString stringWithFormat:@"select breed from AnimalsTable where mainBreed='%@'",trimString]; 
    const char *sql = [select_sql UTF8String]; 
    sqlite3_stmt *selectstmt; 
    if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) { 

     while(sqlite3_step(selectstmt) == SQLITE_ROW) { 

      NSString *animalBreed = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)]; 
      NSLog(@"Breed:%@",animalBreed); 
     } 
    } 
} 
else 
    sqlite3_close(database); 

它正在工作! 感謝上帝!

2
select breed from AnimalsTable where mainBreed=\"%@\"",trimString 

您選擇表中的一列,但

NSString *animalBreed = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 2); 

在那裏你想要得到的第3列,以便改變以2比0象下面這樣:

NSString *animalBreed = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 0); 
+0

感謝您的建議,我對代碼進行了更改。但問題仍然沒有解決。問題在於準備聲明。 – anjum 2012-04-17 08:18:49

1

它不應該be

sqlite3_prepare_v2(database,select_stmt,-1,&selectStatement,NULL); 

它應該是

sqlite3_prepare_v2(database,[selectSQL UTF8String],-1,&selectStatement,NULL); 
+0

謝謝,但問題沒有解決您所建議的更改。 – anjum 2012-04-17 08:32:53

1

嘗試這一點,並檢查你的數據庫路徑 : -

NSString *trimString=[appDelegate.mainBreedSelected stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

sqlite3 *database; 

NSString *selectSQL=[NSString stringWithFormat:@"select breed from AnimalsTable where mainBreed ='%@'",trimString]; 
sqlite3_stmt *selectStatement; 
if (sqlite3_open([appDelegate.sqlFile UTF8String],&database)==SQLITE_OK) 
   { 
        sqlite3_prepare_v2(database,[selectSQL cStringUsingEncoding:NSUTF8StringEncoding],-1,&selectStatement,NULL); 
        if (sqlite3_step(selectStatement)==SQLITE_DONE) 
            { 
         while(sqlite3_step(selectStatement) == SQLITE_ROW) 
             { 
          NSString *animalBreed = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 0)]; 
            NSLog(@"Breed:%@",animalBreed); 
             } 
        } 
        else 
         NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(database)); 
     sqlite3_finalize(selectStatement); 
     sqlite3_close(database); 
    } 
    else 
     NSLog(@"Database cannot be opened"); 

,並沒有檢查你列在抓取數據庫sqlite3_column_text(selectStatement, 0)];

+0

謝謝,但問題仍然保持不變。 – anjum 2012-04-17 08:33:35

+0

你有probelem在這一行請使用此: - NSString * animalBreed = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement,0)]; – Deepesh 2012-04-17 08:38:01

+0

我也做了這些改變。我已經在appDelegate中檢索我的數據庫路徑。並在另一個視圖插入值的工作正常,我不明白什麼問題與選擇語句。 – anjum 2012-04-17 08:43:16