在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”,原因:“选择时出错。”
如何解决这个问题?
发布于 2012-04-17 09:44:49
我改变了密码:
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-17 08:14:29
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-17 08:21:38
不应该是
sqlite3_prepare_v2(database,select_stmt,-1,&selectStatement,NULL);它应该是
sqlite3_prepare_v2(database,[selectSQL UTF8String],-1,&selectStatement,NULL);https://stackoverflow.com/questions/10187236
复制相似问题