2012-04-17 78 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