gpt4 book ai didi

iphone - 执行select语句时发生未知错误

转载 作者:行者123 更新时间:2023-12-03 18:27:01 24 4
gpt4 key购买 nike

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

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


在我必须从数据库中选择值的其中一个viewcontroller中,代码如下:

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'而终止应用程序,
原因:“选择时出错。
'未知错误''


如何解决这个问题?

最佳答案

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

关于iphone - 执行select语句时发生未知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10187236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com