1

在 AppDelegate 中,我检索了数据库的路径:

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

我收到错误消息:

*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“选择时出错。'未知错误''

如何解决这个问题?

4

4 回答 4

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);
于 2012-04-17T08:14:29.387 回答
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); 

它正在工作!感谢上帝!!!

于 2012-04-17T09:44:49.117 回答
1

不应该

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

它应该是

sqlite3_prepare_v2(database,[selectSQL UTF8String],-1,&selectStatement,NULL);
于 2012-04-17T08:21:38.300 回答
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");

并在 fetch 数据库中检查您的列号sqlite3_column_text(selectStatement, 0)];

于 2012-04-17T08:24:07.940 回答