Ios 使用未声明的标识符';sqlStatement';
您好,我对iOS开发非常陌生,我正在使用教程的代码,如下所示: 但我正在改变它的运行方式。但有些人会犯一些错误,即使教程和我有相同的代码。 以下是教程中的代码:Ios 使用未声明的标识符';sqlStatement';,ios,objective-c,sqlite,xcode5,Ios,Objective C,Sqlite,Xcode5,您好,我对iOS开发非常陌生,我正在使用教程的代码,如下所示: 但我正在改变它的运行方式。但有些人会犯一些错误,即使教程和我有相同的代码。 以下是教程中的代码: -(NSMutableArray *) authorList{ theauthors = [[NSMutableArray alloc] initWithCapacity:10]; @try { NSFileManager *fileMgr = [NSFileManager defaultManager]
-(NSMutableArray *) authorList{
theauthors = [[NSMutableArray alloc] initWithCapacity:10];
@try {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"AuthorsDb.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured: %@", sqlite3_errmsg(db));
}
const char *sql = "SELECT * FROM books";
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db));
}else{
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
Author * author = [[Author alloc] init];
author.name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
author.title = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];
author.genre = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)];
[theauthors addObject:author];
}
}
}
@catch (NSException *exception) {
NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db));
}
@finally {
sqlite3_finalize(sqlStatement);
sqlite3_close(db);
return theauthors;
}
}
这是我的密码:
-(NSMutableArray *) Servicelist{
theservices = [[NSMutableArray alloc] initWithCapacity:10];
@try {
// CHANGE THE NAME OF THE DATABASE
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"DBManicurious.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured: %@", sqlite3_errmsg(db));
}
//ALTER THE SELECTION
const char *sql = "SELECT ServiceName, Price FROM tbl_Services WHERE TitleID=1"; //the Table view will be populated by the type ID
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db));
}else{
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
//This one must be altered .. :)
/*must recode this as soon as you get this -->>*/
services1 *services = [[services1 alloc] init];
services.title = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,0)];
// This one is an integer so find a way and recode this as you get this
services.price = sqlite3_column_int(sqlStatement, 1);
// author.genre = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)];
[theservices addObject:services];
}
}
}
@catch (NSException *exception) {
NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db));
}
@finally {
sqlite3_finalize(sqlStatement);
sqlite3_close(db);
return theservices;
}
}
我在“sqlite3_finalize(sqlStatement);”中得到错误
它说:使用未声明的标识符“sqlStatement”您已声明
sqlite3_stmt *sqlStatement;
在@try
块中,使其作用域在那里。将声明移到@try
之外,作为奖励,将其初始化为零,例如
sqlite3_stmt *sqlStatement = 0;
@try { ...